首页

关于oschrenk-util源码包Zip打包工具类对指定文件路径文件进行zip压缩/unzip解压处理源码说明

标签:oschrenk-util,Zip打包工具类,zip解压,unzip压缩     发布时间:2018-07-23   

一、前言

关于oschrenk-util源码包中com.oschrenk.io.Zip打包工具类(依赖com.oschrenk.utils.IOUtils类),实现指定路径文件zip压缩/解压unzip处理、追加指定文件路径进行addToZip打包文件、追加文件夹文件打包addFolderToZip文件,详情参见源码说明。

二、源码说明  

package com.oschrenk.io;@b@@b@import com.oschrenk.utils.IOUtils;@b@import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.Closeable;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.util.Enumeration;@b@import java.util.List;@b@import java.util.Vector;@b@import java.util.zip.ZipEntry;@b@import java.util.zip.ZipFile;@b@import java.util.zip.ZipOutputStream;@b@@b@public class Zip@b@{@b@  public static void unzip(File archive, File destDir)@b@    throws IOException@b@  {@b@    if (!(destDir.exists())) {@b@      destDir.mkdir();@b@    }@b@@b@    ZipFile zipFile = new ZipFile(archive);@b@@b@    byte[] buffer = new byte[16384];@b@@b@    for (ZipEntry entry : getZipEntries(zipFile)) {@b@      String entryFileName = entry.getName();@b@      File dir = buildDirectoryHierarchyFor(entryFileName, destDir);@b@@b@      if (!(dir.exists()))@b@        dir.mkdirs();@b@@b@      if (!(entry.isDirectory())) {@b@        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destDir, entryFileName)));@b@@b@        BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));@b@@b@        while ((len = bis.read(buffer)) > 0) {@b@          int len;@b@          bos.write(buffer, 0, len);@b@        }@b@        IOUtils.close(new Closeable[] { bos });@b@        IOUtils.close(new Closeable[] { bis });@b@      }@b@    }@b@  }@b@@b@  private static List<ZipEntry> getZipEntries(ZipFile zipFile)@b@  {@b@    List zipEntries = new Vector();@b@    Enumeration entries = zipFile.entries();@b@    if (entries.hasMoreElements())@b@      zipEntries.add((ZipEntry)entries.nextElement());@b@@b@    return zipEntries;@b@  }@b@@b@  private static File buildDirectoryHierarchyFor(String entryName, File destDir)@b@  {@b@    String internalPathToEntry = entryName.substring(0, entryName.lastIndexOf(47) + 1);@b@@b@    return new File(destDir, internalPathToEntry);@b@  }@b@@b@  public static void zip(File src, File zipFile)@b@    throws IOException@b@  {@b@    ZipOutputStream zip = null;@b@    try {@b@      zip = new ZipOutputStream(new FileOutputStream(zipFile));@b@@b@      if (src.isDirectory())@b@        addFolderToZip(new File(""), src, zip);@b@      else@b@        addToZip(new File(""), src, zip);@b@    }@b@    finally {@b@      IOUtils.close(new Closeable[] { zip });@b@    }@b@  }@b@@b@  private static void addToZip(File path, File srcFile, ZipOutputStream zip)@b@    throws IOException@b@  {@b@    if (srcFile.isDirectory()) {@b@      addFolderToZip(path, srcFile, zip);@b@    }@b@    else {@b@      byte[] buf = new byte[1024];@b@@b@      FileInputStream in = null;@b@      try {@b@        in = new FileInputStream(srcFile);@b@        zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName()));@b@        while ((len = in.read(buf)) > 0) {@b@          int len;@b@          zip.write(buf, 0, len);@b@        }@b@      } finally {@b@        IOUtils.close(new Closeable[] { in });@b@      }@b@    }@b@  }@b@@b@  private static void addFolderToZip(File path, File srcFolder, ZipOutputStream zip)@b@    throws IOException@b@  {@b@    File[] arr$ = srcFolder.listFiles(); int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { File file = arr$[i$];@b@      addToZip(new File(path.getPath() + File.separator + srcFolder.getName()), new File(srcFolder.getPath() + File.separator + file.getName()), zip);@b@    }@b@  }@b@}