Zlib 压缩 在 Java 中使用 Deflate 和 Inflate 类
我想尝试使用 java.util.zip 中的 Deflate 和 Inflate 类进行 zlib 压缩。
我可以使用 Deflate 压缩代码,但在解压缩时出现此错误 -
Exception in thread "main" java.util.zip.DataFormatException: unknown compression method
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:238)
at java.util.zip.Inflater.inflate(Inflater.java:256)
at zlibCompression.main(zlibCompression.java:53)
这是迄今为止我的代码 -
import java.util.zip.*;
import java.io.*;
public class zlibCompression {
/**
* @param args
*/
public static void main(String[] args) throws IOException, DataFormatException {
// TODO Auto-generated method stub
String fname = "book1";
FileReader infile = new FileReader(fname);
BufferedReader in = new BufferedReader(infile);
FileOutputStream out = new FileOutputStream("book1out.dfl");
//BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
Deflater compress = new Deflater();
Inflater decompress = new Inflater();
String readFile = in.readLine();
byte[] bx = readFile.getBytes();
while(readFile!=null){
byte[] input = readFile.getBytes();
byte[] compressedData = new byte[1024];
compress.setInput(input);
compress.finish();
int compressLength = compress.deflate(compressedData, 0, compressedData.length);
//System.out.println(compressedData);
out.write(compressedData, 0, compressLength);
readFile = in.readLine();
}
File abc = new File("book1out.dfl");
InputStream is = new FileInputStream("book1out.dfl");
InflaterInputStream infl = new InflaterInputStream(new FileInputStream("book1out.dfl"), new Inflater());
FileOutputStream outFile = new FileOutputStream("decompressed.txt");
byte[] b = new byte[1024];
while(true){
int a = infl.read(b,0,1024);
if(a==0)
break;
decompress.setInput(b);
byte[] fresult = new byte[1024];
//decompress.in
int resLength = decompress.inflate(fresult);
//outFile.write(b,0,1);
//String outt = new String(fresult, 0, resLength);
//System.out.println(outt);
}
System.out.println("complete");
}
}
I want trying to use the Deflate and Inflate classes in java.util.zip for zlib compression.
I am able to compress the code using Deflate, but while decompressing, I am having this error -
Exception in thread "main" java.util.zip.DataFormatException: unknown compression method
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:238)
at java.util.zip.Inflater.inflate(Inflater.java:256)
at zlibCompression.main(zlibCompression.java:53)
Here is my code so far -
import java.util.zip.*;
import java.io.*;
public class zlibCompression {
/**
* @param args
*/
public static void main(String[] args) throws IOException, DataFormatException {
// TODO Auto-generated method stub
String fname = "book1";
FileReader infile = new FileReader(fname);
BufferedReader in = new BufferedReader(infile);
FileOutputStream out = new FileOutputStream("book1out.dfl");
//BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
Deflater compress = new Deflater();
Inflater decompress = new Inflater();
String readFile = in.readLine();
byte[] bx = readFile.getBytes();
while(readFile!=null){
byte[] input = readFile.getBytes();
byte[] compressedData = new byte[1024];
compress.setInput(input);
compress.finish();
int compressLength = compress.deflate(compressedData, 0, compressedData.length);
//System.out.println(compressedData);
out.write(compressedData, 0, compressLength);
readFile = in.readLine();
}
File abc = new File("book1out.dfl");
InputStream is = new FileInputStream("book1out.dfl");
InflaterInputStream infl = new InflaterInputStream(new FileInputStream("book1out.dfl"), new Inflater());
FileOutputStream outFile = new FileOutputStream("decompressed.txt");
byte[] b = new byte[1024];
while(true){
int a = infl.read(b,0,1024);
if(a==0)
break;
decompress.setInput(b);
byte[] fresult = new byte[1024];
//decompress.in
int resLength = decompress.inflate(fresult);
//outFile.write(b,0,1);
//String outt = new String(fresult, 0, resLength);
//System.out.println(outt);
}
System.out.println("complete");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想在这里做什么?您使用 InflaterInputStream 来解压缩您的数据,然后您尝试再次将此解压缩的数据传递给 Inflater?使用其中之一,但不能同时使用两者。
这就是导致您出现异常的原因。
除此之外,还有相当多的小错误,比如bestsss提到的这些:
a
的情况下设置 Inflater 的输入。我发现的更多内容:
我不会尝试纠正你的程序。这是一个简单的方法,它使用 DeflaterOutputStream 和 InflaterInputStream 完成我认为您想要的操作。 (您也可以使用 JZlib 的 ZInputStream 和 ZOutputStream 来代替。)
为了提高效率,用缓冲流包装文件流可能会很有用。如果这对性能至关重要,请对其进行测量。
What are you trying to do here? You use an InflaterInputStream, which decompresses your data, and then you try to pass this decompressed data again to an Inflater? Use either one of them, but not both.
This is what is causing your exception here.
In addition to this, there are quite some minor errors, like these mentioned by bestsss:
a
, too.Some more which I found:
readLine()
to read a line of text, but then you don't add the line break again, which means in your decompressed file won't be any line breaks.I won't try to correct your program. Here is a simple one which does what I think you want, using DeflaterOutputStream and InflaterInputStream. (You could also use JZlib's ZInputStream and ZOutputStream instead.)
For more efficiency, it might be useful to wrap the file streams with buffered streams. If this is performance critical, measure it.
Paŭlo Ebermann 的代码可以通过使用 try-with-resources:
使用
Zlib
的另一种方式确实是使用Inflater
和Deflater
类:Paŭlo Ebermann's code can be further improved by using try-with-resources:
Also another way of using
Zlib
is indeed with theInflater
andDeflater
classes: