用Java创建zip存档 [英] Creating zip archive in Java
问题描述
我有一个由7zip程序创建的文件。我使用 deflate 方法来压缩它。现在我想在 java
中创建相同的存档(具有相同的MD5sum)。当我创建zip文件时,我使用了我在互联网上找到的算法,例如 http:// www.kodejava.org/examples/119.html 但是当我使用这种方法创建zip文件时,压缩的大小高于未压缩文件的大小,那么发生了什么?这不是一个非常有用的压缩。那么如何创建与我用7zip程序创建的zip文件完全相同的zip文件呢?如果它有助于我获得有关我在7zip程序中创建的zip文件的所有信息。
I have one file created by 7zip program. I used deflate method to compress it. Now I want to create the same archive (with the same MD5sum) in java
. When I create zip file, I used the algorithm that I found on the Internet for example http://www.kodejava.org/examples/119.html but when I created zip file with this method the compressed size is higher than size of the uncompressed file so what is going on? This isn't a very useful compression. So how I can create zip file that is exactly same as zip file that I created with 7zip program ? If it helps I have all information about zip file that I created in 7zip program.
推荐答案
// simplified code for zip creation in java
import java.io.*;
import java.util.zip.*;
public class ZipCreateExample {
public static void main(String[] args) throws Exception {
// input file
FileInputStream in = new FileInputStream("F:/sometxt.txt");
// out put file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip"));
// name the file inside the zip file
out.putNextEntry(new ZipEntry("zippedjava.txt"));
// buffer size
byte[] b = new byte[1024];
int count;
while ((count = in.read(b)) > 0) {
out.write(b, 0, count);
}
out.close();
in.close();
}
}
这篇关于用Java创建zip存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!