在 linux下进行 zip,然后在 linux 下 unzip或者直接下载,中文的文件名会乱码。
我查了一下,可以通过 entry.setUnixMode 的方式,将 platform 设置为 Unix,如下:
- public void setUnixMode(int mode) {
- // CheckStyle:MagicNumberCheck OFF - no point
- setExternalAttributes((mode << 16)
- // MS-DOS read-only attribute
- | ((mode & 0200) == 0 ? 1 : 0)
- // MS-DOS directory flag
- | (isDirectory() ? 0x10 : 0));
- // CheckStyle:MagicNumberCheck ON
- platform = PLATFORM_UNIX;
- }
public void setUnixMode(int mode) {
// CheckStyle:MagicNumberCheck OFF - no point
setExternalAttributes((mode << 16)
// MS-DOS read-only attribute
| ((mode & 0200) == 0 ? 1 : 0)
// MS-DOS directory flag
| (isDirectory() ? 0x10 : 0));
// CheckStyle:MagicNumberCheck ON
platform = PLATFORM_UNIX;
}
这样在解压的时候,就不需要绕过去了。
另外:要使用 ant 的 zip class,才能解决中文文件名乱码问题,JDK默认的我暂时还不知道如何做。
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipOutputStream;
- public static void write(File path, File zipFile) throws IOException {
- ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
- zip.setEncoding("GBK");
- Util.write(path, path, zip);
- zip.close();
- }
- private static void write(File base, File path, ZipOutputStream zip) throws IOException {
- URI rel = base.toURI().relativize(path.toURI());
- if (path.isDirectory()) {
- ZipEntry entry = new ZipEntry(rel.getPath());
- entry.setUnixMode(755);
- zip.putNextEntry(entry);
- zip.closeEntry();
- File[] files = path.listFiles();
- for (File file : files) {
- write(base, file, zip);
- }
- } else {
- ZipEntry entry = new ZipEntry(rel.getPath());
- entry.setUnixMode(644);
- zip.putNextEntry(entry);
- FileInputStream is = new FileInputStream(path);
- zip.write(IOUtils.toByteArray(is));
- is.close();
- zip.closeEntry();
- }
- }