gpt4 book ai didi

java - 如何使用UTF-8编码打开java程序生成的zip文件

转载 作者:搜寻专家 更新时间:2023-11-01 03:28:45 27 4
gpt4 key购买 nike

我们的产品有一个导出功能,它使用ZipOutputStream来压缩一个目录;但是,当您尝试压缩包含带有中文或日文字符的文件名的目录时,导出无法正常工作。由于某种原因,压缩文件中的新文件的名称不同。这是我们的邮政编码示例:

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
out.setEncoding("UTF-8");
//program to add directory to zip
//program add/create file to zip
out.close();

我的导入算法也是用 Java 构建的,可以正确导入压缩文件,即使文件/目录名称中包含中文/日文字符也是如此。

 Zipfile zipfile = new ZipFile(zipPath, "UTF-8");
Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
String name = entry.getName();
....

zip 软件的程序是否在解压缩 UTF-8 编码的文件时遇到问题,或者是否需要一些特殊的东西来创建可以由使用 utf-8 编码的现有软件轻松使用的 zip 文件?


我写了一个示例程序:

package ZipFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipFolder{
public static void main(String[] a) throws Exception
{
String srcFolder = "D:/9.4_work/openscript_repo/中文124.All/中文";
String destZipFile = "D:/Eclipse_Projects/OpenScriptDebuggingProject/src/ZipFile/demo.zip";
zipFolder(srcFolder, destZipFile);
}

static public void zipFolder(String srcFolder, String destZipFile) throws Exception
{
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;

fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
zip.setEncoding("UTF-8");
// using GBK encoding, the chinese name can be correctly displayed when unzip
// zip.setEncoding("GBK");

addFolderToZip("", srcFolder, zip);
zip.flush();
zip.close();
}

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception
{

File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
}
else {
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
}

static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception
{
File folder = new File(srcFolder);

for (String fileName : folder.list()) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
}
else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
}
}
}

最佳答案

此处的最佳答案可能会回答您的问题;不幸的是,它似乎暗示 Zip 格式并不能真正允许创建将在任何计算机上正确显示文件名的 Zip 文件:

https://superuser.com/questions/60379/linux-zip-tgz-filenames-encoding-problem

我希望当您将编码设置为 GBK 时它会起作用,因为这是您系统的默认编码,因此 7zip 将其用于它打开的所有 zip 文件。

这表明rar7z格式有更好的支持。

我在 Java 压缩包中发现了一篇专门介绍 UTF-8 的博客文章。它表明有一个更新版本的 ZIP 规范,当前版本的 Java 可能不会创建,但 Java 7 会创建。我不知道 Apache 类是否也使用它。

http://blogs.oracle.com/xuemingshen/entry/non_utf_8_encoding_in

关于java - 如何使用UTF-8编码打开java程序生成的zip文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263141/

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