我试图做一个程序来压缩文件是.tar.gz:的tar.gz压缩没有为GIF文件工作
下面是代码:
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
public class Compress {
public static void main(String[] args) {
BufferedInputStream input = null;
try {
input = new BufferedInputStream(new FileInputStream(new File("input_filename.filetype")));
TarArchiveOutputStream out = null;
try {
out = new TarArchiveOutputStream(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream("output_filename.tar.gz"))));
out.putArchiveEntry(new TarArchiveEntry(new File("input_filename.filetype")));
int count;
byte data[] = new byte[input.available()];
while ((count = input.read(data)) != -1) {
out.write(data, 0, count);
}
input.close();
} catch (IOException ex) {
Logger.getLogger(Compress.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (out != null) {
try {
out.closeArchiveEntry();
out.close();
} catch (IOException ex) {
Logger.getLogger(Compress.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Compress.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
input.close();
} catch (IOException ex) {
Logger.getLogger(Compress.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
我使用Apache Commons Compression作为库。
我有2个条件测试:
- 压缩GIF文件
- 压缩PDF文件
而且我比较使用PeaZip压缩,这里是结果:
如果输入的文件,如果我们使用PeaZip是GIF的压缩文件增加的大小,相同的。但是对于其他文件,它适用于压缩过程。
任何人都可以解释这是怎么回事?我的代码有问题吗?
谢谢您的帮助...
欢迎来到SO。 +1,写得很好,写得很好。我也许也应该-1,因为它是一个非常有名的压缩特性,它只能使用一次。正如所有答案指出的那样,压缩已压缩的东西可能会使其变大,而不是变小。但我会给你-1的疑问的好处;-) – 2012-04-11 03:25:20
有没有什么建议如果我想在不降低尺寸的情况下缩小GIF文件大小,我该怎么办?只是一个关键字,它会对我有所帮助:) – Crazenezz 2012-04-11 03:34:39
正如所有的答案所说,你不能这样做。 GIF文件已经被压缩了,因为它们可以得到。你可能会发现一种异常的压缩算法,可以再挤1%,但这就是你将得到的所有东西。这是不值得的努力。如果你想要更多的压缩,你应该检查低质量设置的JPEG。 – 2012-04-11 03:39:31