zip打包工具类
来源:互联网 发布:光环大数据培训官网 编辑:程序博客网 时间:2024/06/07 06:39
游戏热更新新资源打包成zip的方法
zip工具类:
package com.xysd.xmx.utils; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream; /** * ZIP压缩工具 * @author denghuahui * */ public class ZipUtils { public static final String EXT = ".zip"; private static final String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符 private static final String PATH = "/"; private static final int BUFFER = 1024; /** * 单个文件压缩,压缩后的路径为源文件同路径,名字为 《原名.后缀.zip》 * @param srcPath 源文件路径 * @throws Exception */ public static void compress(String srcPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile); } /** * 单个文件压缩,压缩后的路径为源文件同路径,名字为 《原名.后缀.zip》 * @param srcFile 源文件 * @throws Exception */ public static void compress(File srcFile) throws Exception { String name = srcFile.getName(); String basePath = srcFile.getParent(); String destPath = basePath + name + EXT; compress(srcFile, destPath); } /** * 文件压缩 * @param srcPath * 源文件路径 * @param destPath * 目标文件路径 */ public static void compress(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile, destPath); } /** * 压缩 * * @param srcFile * 源路径 * @param destPath * 目标路径 * @throws Exception */ public static void compress(File srcFile, File destFile) throws Exception { // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); compress(srcFile, zos, BASE_DIR); zos.flush(); zos.close(); } /** * 压缩文件 * * @param srcFile * @param destPath * @throws Exception */ public static void compress(File srcFile, String destPath) throws Exception { compress(srcFile, new File(destPath)); } /** * 压缩一堆文件 * @param files 文件列表 * @param destFile 目标文件路径 d:/tmep/aa.zip aa.zip将作为压缩文件的文件名 * @throws Exception */ public static void compressFileLists(List<File> files , String destFilePath) throws Exception{ File destFile = new File(destFilePath); // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); for(File file : files){ compress(file, zos, ""); } zos.flush(); zos.close(); } /** * 压缩一堆文件 * @param files 文件列表 * @param destFile 目标文件路径 d:/tmep/aa.zip aa.zip将作为压缩文件的文件名 * @throws Exception */ public static void compressFileLists(File[] files , String destFilePath) throws Exception{ File destFile = new File(destFilePath); // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); for(File file : files){ compress(file, zos, file.getName()); } zos.flush(); zos.close(); } /** * 压缩一堆文件 * @param files 文件列表 * @param destFile 目标文件 * @throws Exception */ public static void compressFileLists(File[] files , File destFile) throws Exception{ // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); for(File file : files){ compress(file, zos, file.getName()); } zos.flush(); zos.close(); } /** * 压缩一堆文件 * @param files 文件列表 * @param destFile 目标文件 * @throws Exception */ public static void compressFileLists(List<File> files , File destFile) throws Exception{ // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); for(File file : files){ compress(file, zos, file.getName()); } zos.flush(); zos.close(); } /*************--------------------*****************-----------------------************/ /** * 压缩 * * @param srcFile * 源路径 * @param zos * ZipOutputStream * @param basePath * 压缩包内相对路径 * @throws Exception */ private static void compress(File srcFile, ZipOutputStream zos, String basePath) throws Exception { if (srcFile.isDirectory()) { compressDir(srcFile, zos, basePath); } else { compressFile(srcFile, zos, basePath); } } /**************----------------*************---------------****************/ /** * 压缩目录 * @param dir * @param zos * @param basePath * @throws Exception */ private static void compressDir(File dir, ZipOutputStream zos, String basePath) throws Exception { File[] files = dir.listFiles(); // 构建空目录 if (files.length < 1) { ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH); zos.putNextEntry(entry); zos.closeEntry(); } for (File file : files) { // 递归压缩 compress(file, zos, basePath + dir.getName() + PATH); } } /** * 文件压缩 * * @param file * 待压缩文件 * @param zos * ZipOutputStream * @param dir * 压缩文件中的当前路径 * @throws Exception */ private static void compressFile(File file, ZipOutputStream zos, String dir) throws Exception { /** * 压缩包内文件名定义 * <pre> * 如果有多级目录,那么这里就需要给出包含目录的文件名 * 如果用WinRAR打开压缩包,中文名将显示为乱码 * </pre> */ ZipEntry entry = new ZipEntry(dir + file.getName()); //对应压缩包里每一个文件(实体) zos.putNextEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream( file)); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zos.write(data, 0, count); } bis.close(); zos.closeEntry(); } /** * 压缩一堆文件 * @param files 文件列表 * @param destFile 目标文件路径 d:/tmep/aa.zip aa.zip将作为压缩文件的文件名 * @throws Exception */ public static void compressFileLists2(List<File> files , String destFilePath) throws Exception{ File destFile = new File(destFilePath); // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); for(File file : files){ compress(file, zos, ""); } zos.flush(); zos.close(); } public static void main(String[] args) { // 压缩文件 // try {////ZipUtils.compress("d:\\f.txt");//// 压缩目录 // List<File> files = new ArrayList<File>();// File file1 = new File("E:\\apache-tomcat-7.0.61\\webapps\\xmx\\resource\\110120_2_1_body.png");// File file2 = new File("E:\\apache-tomcat-7.0.61\\webapps\\xmx\\resource\\110120_2_1_df.txt");// File file3 = new File("E:\\apache-tomcat-7.0.61\\webapps\\xmx\\resource\\110120_2_1_head.xlsx");// File file4 = new File("E:\\apache-tomcat-7.0.61\\webapps\\xmx\\resource\\110120_2_1_xmx.jpg");// files.add(file1);// files.add(file2);// files.add(file3);// files.add(file4);// File destFile = new File("E:\\apache-tomcat-7.0.61\\webapps\\xmx\\resource\\temp\\aaa.zip");// ZipUtils.compressFileLists(files,destFile);//} catch (Exception e) {//e.printStackTrace();//} try {// 压缩目录 List<File> files = new ArrayList<File>(); File file1 = new File("D:\\loadingLogo.png"); files.add(file1); File destFile = new File("D:\\aa.zip"); ZipUtils.compressFileLists(files,destFile);} catch (Exception e) {e.printStackTrace();}}}
0 0
- zip打包工具类
- zip、unzip工具类
- Zip解压工具类
- java zip工具类
- java zip 工具类
- java zip 工具类
- ZIP工具类
- zip工具类
- Java zip工具类
- java zip工具类
- ZIP工具类
- java将文件打包成ZIP压缩文件的工具类实例
- java语言 ant.jar zip打包 utils工具类 通用性强
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- java将文件打包成ZIP压缩文件的工具类实例
- CompressFile工具类使用,zip和rar文件解压缩与打包操作Java源代码
- java写的将文件或文件夹打包成zip的工具类
- Android MultiDex 解析与使用
- 浅入浅出 Android 安全 翻译完成!
- Tomcat 部署详解
- 多线程练习题(二)
- Ubuntu16.04配置JDK1.8.0环境。
- zip打包工具类
- 访问者模式
- 微信支付返回-1,微信填坑
- 测试地址
- The Snail
- HDU1873 看病要排队(优先队列priority_queue)
- 模板方法模式例子
- iOS 百度地图SDK使用漫谈
- Docker的安装配置及使用详解