使用JAVA本身的ZIP API打包带有中文名称的文件(目录)会出现乱码,且winzip无法解压,如果要解决此问题,可以将
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
换为ant.jar中的API
/** * 压缩文件(.zip)的函数 * @param zipDirectory:(需要)压缩的文件夹路径 * @param zipPath:文件压缩后放置的路径,该路径可以为null,null表示压缩到原文件的同级目录 * @return :返回一个压缩好的文件(File),否则返回null */ public File doZip(String zipDirectory, String zipPath) { File zipDir = new File(zipDirectory); if (zipPath == null) { zipPath = zipDir.getParent(); } // 压缩后生成的zip文件名 String zipFileName = zipPath + "/" + zipDir.getName() + ".zip"; try { zipOut = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(zipFileName))); // 压缩文件 handleDir(zipDir, zipDir.getParent().length() + 1, zipOut); zipOut.close(); return new File(zipFileName); } catch (IOException e) { logger.error(e); e.printStackTrace(); return null; } } /** * 由doZip调用,递归完成目录文件读取 * @param dir:(需要)压缩的文件夹(File 类型) * @param len:一个参数(记录压缩文件夹的parent路径的长度) * @param zipOut:需要压缩进的压缩文件 * @throws IOException:如果出错,会抛出IOE异常 */ private void handleDir(File dir, int len, ZipOutputStream zipOut) throws IOException { FileInputStream fileIn = null; File[] files = dir.listFiles(); if (files != null) { if (files.length > 0) { // 如果目录不为空,则分别处理目录和文件. for (File fileName : files) { if (fileName.isDirectory()) { handleDir(fileName, len, zipOut); } else { fileIn = new FileInputStream(fileName); zipOut.putNextEntry(new ZipEntry(fileName.getPath() .substring(len).replaceAll("\\\\", "/"))); while ((readedBytes = fileIn.read(buf)) > 0) { zipOut.write(buf, 0, readedBytes); } zipOut.closeEntry(); } } } else { // 如果目录为空,则单独创建之. zipOut.putNextEntry(new ZipEntry(dir.getPath().substring(len) + "/")); zipOut.closeEntry(); } } else {// 如果是一个单独的文件 fileIn = new FileInputStream(dir); zipOut.putNextEntry(new ZipEntry(dir.getPath().substring(len))); while ((readedBytes = fileIn.read(buf)) > 0) { zipOut.write(buf, 0, readedBytes); } zipOut.closeEntry(); } } /** * 解压指定zip文件 * @param unZipfileName:需要解压的zip文件路径 * @param unZipPath:文件解压的路径,该路径可以为null,null表示解压到原文件的同级目录 */ public boolean unZip(String unZipfileName, String unZipPath) {// unZipfileName需要解压的zip文件名 FileOutputStream fileOut = null; InputStream inputStream = null; File file = null; if (unZipPath == null) { unZipPath = new File(unZipfileName).getParent(); // System.out.println("1 -> " + unZipPath); if (!(unZipPath.substring(unZipPath.length()).endsWith("/") || unZipPath .substring(unZipPath.length()).endsWith("\\"))) { unZipPath += "/"; } } else { unZipPath = new File(unZipPath).getPath() + "/"; } try { zipFile = new ZipFile(unZipfileName); for (Enumeration<?> entries = zipFile.getEntries(); entries .hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); int iTEMP = entry.getName().indexOf("/") + 1; file = new File(unZipPath + entry.getName().substring(iTEMP, entry.getName().length())); if (entry.isDirectory()) { file.mkdirs(); } else { // 如果指定文件的目录不存在,则创建之. File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(file); while ((this.readedBytes = inputStream.read(this.buf)) > 0) { fileOut.write(this.buf, 0, this.readedBytes); } fileOut.close(); inputStream.close(); } } this.zipFile.close(); return true; } catch (IOException ioe) { logger.error(ioe); ioe.printStackTrace(); return false; } }