我有byte[]
的zip文件。我必须在不创建新文件的情况下解压缩它,并获取该解压缩文件的byte[]
。
请帮我做那个
答案 0 :(得分:7)
您可以使用ZipInputStream
和ZipOutputStream
(在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;
}