将zip byte []转换为解压缩byte []

时间:2012-02-09 10:48:32

标签: java

我有byte[]的zip文件。我必须在不创建新文件的情况下解压缩它,并获取该解压缩文件的byte[]。 请帮我做那个

3 个答案:

答案 0 :(得分:7)

您可以使用ZipInputStreamZipOutputStream(在java.util.zip包中)来读取和写入ZIP文件。

如果数据位于字节数组中,则可以从ByteArrayInputStream读取这些数据,或者写入指向输入和输出字节数组的ByteArrayOutputStream

答案 1 :(得分:1)

public static List<ZipEntry> extractZipEntries(byte[] content) throws IOException {
    List<ZipEntry> entries = new ArrayList<>();
   
    ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry entry = null;
    while ((entry = zipStream.getNextEntry()) != null)
    {
        System.out.println( "entry: " + entry );
        ZipOutputStream stream= new ZipOutputStream(new FileOutputStream(new File("F:\\ssd\\wer\\"+entry.getName())));
        stream.putNextEntry(entry);
    }
    zipStream.close();
   
    return entries;
}

答案 2 :(得分:-1)

如果您需要deflate压缩数据而又懒于处理流,则可以使用以下代码:

public byte[] deflate(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    return output;
}