使用 Inflator 而不是 ZipInputStream 从 zip 文件中提取数据
Extracting data from File in zip using Inflator instead of ZipInputStream
我提供了 Base64 编码的 zip 文件字符串。我正在尝试解压缩它读取文件的内容。
限制: 充气机的使用 Class
我试过以下代码,但它抛出异常。
byte[] file = Base64.getDecoder().decode(inputString.getBytes());
Inflater inflater = new Inflater(true);
inflater.setInput(file);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(file.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();
System.out.println(new String(output, StandardCharsets.UTF_8));
new Inflator()
异常
Exception in thread "main" java.util.zip.DataFormatException: incorrect header check
异常 ith new Inflator(true)
Exception in thread "main" java.util.zip.DataFormatException: invalid stored block lengths
但是当我尝试像下面的代码一样使用 ZipInputStream 时,它工作得很好。
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(file));
zipInputStream.getNextEntry();
Scanner sc = new Scanner(zipInputStream);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
我的第一个代码中缺少什么?
Inflater
用于解码 zlib 流,而不是 zip 流。
我提供了 Base64 编码的 zip 文件字符串。我正在尝试解压缩它读取文件的内容。
限制: 充气机的使用 Class
我试过以下代码,但它抛出异常。
byte[] file = Base64.getDecoder().decode(inputString.getBytes());
Inflater inflater = new Inflater(true);
inflater.setInput(file);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(file.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();
System.out.println(new String(output, StandardCharsets.UTF_8));
new Inflator()
异常Exception in thread "main" java.util.zip.DataFormatException: incorrect header check
异常 ith new Inflator(true)
Exception in thread "main" java.util.zip.DataFormatException: invalid stored block lengths
但是当我尝试像下面的代码一样使用 ZipInputStream 时,它工作得很好。
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(file));
zipInputStream.getNextEntry();
Scanner sc = new Scanner(zipInputStream);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
我的第一个代码中缺少什么?
Inflater
用于解码 zlib 流,而不是 zip 流。