当社の製品にはZipOutputStream
、ディレクトリを圧縮するために使用するエクスポート機能があります。ただし、中国語または日本語の文字を含むファイル名を含むディレクトリを圧縮しようとすると、エクスポートが正しく機能しません。何らかの理由で、zipファイル内の新しいファイルの名前が異なります。これが私たちのzipコードの例です:
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で構築された私のインポートアルゴリズムは、ファイル/ディレクトリ名に中国語/日本語の文字が含まれている場合でも、zipファイルを正しくインポートできます。
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);
}
}
}
}