gpt4 book ai didi

Java 创建损坏的 ZIP 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:26 25 4
gpt4 key购买 nike

我正在使用 zt-zip通过 zeroturnaround,当我压缩它并尝试打开它时,它说它已损坏。有任何想法吗?ZipUtil.pack(new File("C:\\Users\\David"), new File(zipName));
http://pastie.org/3773634

最佳答案

要制作 Zip 文件,您可以直接使用以下 java 类

导入 java.util.zip.ZipFile;

// These are the files to include in the ZIP file
String[] filenames = new String[]{"filename1", "filename2"};

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try {
// Create the ZIP file
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

// Compress the files
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));

// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Complete the entry
out.closeEntry();
in.close();
}

// Complete the ZIP file
out.close();
} catch (IOException e) {
}

关于Java 创建损坏的 ZIP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10122716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com