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