Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java压缩输入流_Java_Io_Gzip - Fatal编程技术网

用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