用Java压缩输入流
老实说,这不应该有这么多的问题,但我必须错过一些明显的东西 我可以使用用Java压缩输入流,java,io,gzip,Java,Io,Gzip,老实说,这不应该有这么多的问题,但我必须错过一些明显的东西 我可以使用GZIPOutputStream很好地压缩文件,但是当我尝试直接获取输入时(不是从文件,而是从管道或其他地方),当我调用文件的gunzip-d以查看它是否正确解压缩时,它会告诉我它会立即运行到文件末尾。基本上,我希望这些能起作用 echo foo | java Jgzip >foo.gz 或 我建议: OutputStream gout= new GZIPOutputStream( System.out ); Syst
GZIPOutputStream
很好地压缩文件,但是当我尝试直接获取输入时(不是从文件,而是从管道或其他地方),当我调用文件的gunzip-d以查看它是否正确解压缩时,它会告诉我它会立即运行到文件末尾。基本上,我希望这些能起作用
echo foo | java Jgzip >foo.gz
或
我建议:
OutputStream gout= new GZIPOutputStream( System.out );
System.setOut( new PrintStream( gout )); //<<<<< EDIT here
while(( bytesRead = bf.read( buff )) != -1 ) {
gout.write(buff,0,bytesRead);
}
gout.close(); // close flush the last remaining bytes in the buffer stream
OutputStream gout=new GZIPOutputStream(System.out);
系统放样(新打印流(gout))// 你忘了关小溪。只需添加gout.close()代码>在while循环之后使其工作:
axel@loro:~/workspace/Test/bin/tmp$ ls -l
total 12
-rw-rw-r-- 1 axel axel 1328 Oct 27 10:49 JGZip.class
axel@loro:~/workspace/Test/bin/tmp$ echo "hallo" | java JGZip > test.gz
axel@loro:~/workspace/Test/bin/tmp$ ls -l
total 24
-rw-rw-r-- 1 axel axel 1328 Oct 27 10:49 JGZip.class
-rw-rw-r-- 1 axel axel 26 Oct 27 10:49 test.gz
axel@loro:~/workspace/Test/bin/tmp$ gzip -d test.gz
axel@loro:~/workspace/Test/bin/tmp$ ls -l
total 24
-rw-rw-r-- 1 axel axel 1328 Oct 27 10:49 JGZip.class
-rw-rw-r-- 1 axel axel 6 Oct 27 10:49 test
axel@loro:~/workspace/Test/bin/tmp$ cat test
hallo
不幸的是,当gout是OutputStream时,setOut似乎需要一个PrintStream代码>并给@Aubin一个接受勾号!如果您打算稍后将其放入库中,我强烈建议不要使用System.setOut()
。添加了新的PrintStream()@阿克塞尔:你说得对,这种功能(重定向输入/输出/错误)没有发生在库中。但不幸的是,gunzip现在抱怨格式被违反了。至少这比EOF提高了一步。除了下面的答案,我建议使用System.err.println
获取错误消息,否则,您的错误消息将写入gz
文件。
OutputStream gout= new GZIPOutputStream( System.out );
System.setOut( new PrintStream( gout )); //<<<<< EDIT here
while(( bytesRead = bf.read( buff )) != -1 ) {
gout.write(buff,0,bytesRead);
}
gout.close(); // close flush the last remaining bytes in the buffer stream
axel@loro:~/workspace/Test/bin/tmp$ ls -l
total 12
-rw-rw-r-- 1 axel axel 1328 Oct 27 10:49 JGZip.class
axel@loro:~/workspace/Test/bin/tmp$ echo "hallo" | java JGZip > test.gz
axel@loro:~/workspace/Test/bin/tmp$ ls -l
total 24
-rw-rw-r-- 1 axel axel 1328 Oct 27 10:49 JGZip.class
-rw-rw-r-- 1 axel axel 26 Oct 27 10:49 test.gz
axel@loro:~/workspace/Test/bin/tmp$ gzip -d test.gz
axel@loro:~/workspace/Test/bin/tmp$ ls -l
total 24
-rw-rw-r-- 1 axel axel 1328 Oct 27 10:49 JGZip.class
-rw-rw-r-- 1 axel axel 6 Oct 27 10:49 test
axel@loro:~/workspace/Test/bin/tmp$ cat test
hallo