在Java中使用Deflate和Inflate类进行Zlib压缩
|
我想尝试将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\");
}
}
没有找到相关结果
已邀请:
2 个回复
氮顺
来设置充气机的输入。 我发现了一些: 写入后(和从同一文件读取之前),请不要关闭FileOutputStream。 您使用
来读取文本行,但随后不再添加换行符,这意味着在解压缩的文件中将不会有任何换行符。 您可以从字节转换为字符串,然后再转换为字节,而无需任何操作。 您将创建以后不使用的变量。 我不会尝试更正您的程序。这是一个简单的示例,它使用DeflaterOutputStream和InflaterInputStream来满足您的要求。 (您也可以改用JZlib \的ZInputStream和ZOutputStream。)
为了提高效率,用缓冲流包装文件流可能很有用。如果这对性能至关重要,请对其进行测量。
敦肌