【发布时间】:2012-10-27 08:01:37
【问题描述】:
老实说,这不应该有这么多问题,但我肯定遗漏了一些明显的东西。
我可以使用GZIPOutputStream 很好地压缩文件,但是当我尝试直接获取输入时(不是来自文件,而是来自管道或其他东西),当我去调用 gunzip -d 在我的文件上查看它是否正确解压缩,它告诉我它立即运行到文件末尾。基本上,我希望这些工作
echo foo | java Jgzip >foo.gz
或
java Jzip <test.txt >test.gz
而且不能保证这些都是字符串,所以我们逐字节读取。我以为我可以使用System.in and System.out,但这似乎不起作用。
public static void main (String[] args) {
try{
BufferedInputStream bf = new BufferedInputStream(System.in);
byte[] buff = new byte[1024];
int bytesRead = 0;
GZIPOutputStream gout = new GZIPOutputStream (System.out);
while ((bytesRead = bf.read(buff)) != -1) {
gout.write(buff,0,bytesRead);
}
}
catch (IOException ioe) {
System.out.println("IO error.");
System.exit(-1);
}
catch (Throwable e) {
System.out.println("Unexpected exception or error.");
System.exit(-1);
}
}
【问题讨论】:
-
除了下面的答案,我建议使用
System.err.println来获取错误消息,否则,您的错误消息将写入gz文件。