java对 zip文件的压缩和解压(ant解决中文乱码)

您所在的位置:网站首页 jdkzip文件 java对 zip文件的压缩和解压(ant解决中文乱码)

java对 zip文件的压缩和解压(ant解决中文乱码)

2024-07-03 14:56| 来源: 网络整理| 查看: 265

说明:

1、对于压缩的文件,当文件名称是中文时,若使用JDK API中自带的类(java.util.zip.ZipEntry; java.util.zip.ZipOutputStream;)进行压缩,压缩完成后,可以看到压缩包中的文件名称是乱码(文件的内容无乱码),所以使用ANT中的ant.jar中的类(org.apache.tools.zip.ZipEntry; org.apache.tools.zip.ZipOutputStream;)用来解决此问题;

2、解压缩时,如果压缩包中为空,则创建空文件夹

import 如下:

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream;

 

一、压缩文件(ZIP): /** *@name 获取File对象 *@description 相关说明 *@param path 文件路径 *@return File *@throws IOException *@author LJCH *@history 修订历史(历次修订内容、修订人、修订时间等) */ public static File getFile(String path) throws IOException { // 创建文件对象 File file = null; if (path != null && !path.equals("")) { file = new File(path); } if (!file.exists()) { file.createNewFile(); } // 返回文件 return file; } /** *@name 压缩文件 *@description 相关说明 *@param srcFilePath 源文件 *@param destZipFile 目标文件 *@return file 压缩文件的FILE对象 *@throws IOException *@author LJCH *@history 修订历史(历次修订内容、修订人、修订时间等) */ public static File getZipFile(String srcFilePath, String destZipFile) throws IOException { return getZipFile(new File(srcFilePath),destZipFile); } /** *@name 获取压缩文件的File *@description 相关说明 *@param srcFile 源文件 *@param destZipFile 目标文件 *@return file 压缩文件的FILE对象 *@throws IOException *@author LJCH *@history 修订历史(历次修订内容、修订人、修订时间等) */ public static File getZipFile(File srcFile, String destZipFile) throws IOException { final File zipFile = getFile(destZipFile); // 文件输出流 final FileOutputStream outputStream =new FileOutputStream(zipFile); // 压缩流 final ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); if (srcFile.isDirectory()) { directory(zipOutputStream, srcFile); } else { zipFile(srcFile, zipOutputStream); } // 关闭压缩流、文件流 zipOutputStream.close(); outputStream.close(); return zipFile; } /** *@name 递归压缩目录结构 *@description 相关说明 *@param zipOutputStream 压缩文件流 *@param file file *@throws IOException *@author LJCH *@history 修订历史(历次修订内容、修订人、修订时间等) */ public static void directory(ZipOutputStream zipOutputStream, File file) throws IOException { final File[] files = file.listFiles(); for (File fileTemp : files) { if (fileTemp.isDirectory()) { directory(zipOutputStream, fileTemp); } else { zipFile(fileTemp, zipOutputStream); } } if (files.length == 0) { try { zipOutputStream.putNextEntry(new ZipEntry(getPath(file) + File.separator)); } catch (Exception e) { e.printStackTrace(); } } } /** *@name 获得该文件在压缩包中的相对路径 *@description 相关说明 *@param file File *@return String *@author LJCH *@history 修订历史(历次修订内容、修订人、修订时间等) */ public static String getPath(File file) { final String str1 = file.getAbsolutePath(); final int n1 = str1.length(); final String str2 = file.getAbsolutePath(); final int n2 = str2.length(); final String str3 = file.getName(); final int n3 = str3.length(); final String str = str2.substring(n1 - n3, n2); return str; } /** * 将文件数据写入文件压缩流 * @param file 带压缩文件 * @param zipOutputStream 压缩文件流 * @throws IOException */ private static void zipFile(File file, ZipOutputStream zipOutputStream) throws IOException { if (file.exists()) { if (file.isFile()) { final FileInputStream fis = new FileInputStream(file); final BufferedInputStream bis = new BufferedInputStream(fis); final ZipEntry entry = new ZipEntry(file.getName()); zipOutputStream.putNextEntry(entry); final int MAX_BYTE = 10 * 1024 * 1024; // 最大流为10MB long streamTotal = 0; // 接收流的容量 int streamNum = 0; // 需要分开的流数目 int leaveByte = 0; // 文件剩下的字符数 byte[] buffer; // byte数据接受文件的数据 streamTotal = bis.available(); // 获取流的最大字符数 streamNum = (int) Math.floor(streamTotal / MAX_BYTE); leaveByte = (int) (streamTotal % MAX_BYTE); if (streamNum > 0) { for (int i = 0; i < streamNum; i++) { buffer = new byte[MAX_BYTE]; bis.read(buffer, 0, MAX_BYTE); zipOutputStream.write(buffer, 0, MAX_BYTE); } } // 写入剩下的流数据 buffer = new byte[leaveByte]; bis.read(buffer, 0, leaveByte); // 读入流 zipOutputStream.write(buffer, 0, leaveByte); // 写入流 zipOutputStream.closeEntry(); // 关闭当前的zip entry // 关闭输入流 bis.close(); fis.close(); } } } 二、解压缩文件(ZIP) /** *@name 解压zip格式压缩包 *@description 相关说明 *@param sourceZip 源文件 *@param destDir 目标文件地址 *@throws Exception *@author LJCH *@history 修订历史(历次修订内容、修订人、修订时间等) */ @SuppressWarnings("unchecked") private static void unzip(String sourceZip, String destDir) throws Exception { ZipFile zipFile = null; try { final File f = new File(sourceZip); if ((!f.exists()) && (f.length()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3