项目源码可点击获取 (可作为Jar依赖包直接使用)

    1、项目使用前,请进入rdc-bom目录下,执行mvn clean install命令
    2、可根据需要进入rdc-module-file目录,执行mvn clean package命令
    3、打包完成后,可直接引入Jar包使用或上传Maven私服使用。

    public class ZipUtil {
    static Logger logger = LoggerFactory.getLogger(ZipUtil.class);
    private static final int BUFFER_SIZE = 2 * 1024;

    1. _/**<br /> * 对压缩文件进行解压处理<br /> *<br /> * _**_@param _**_zipPath 待解压的压缩包路径,例如压缩包G:/abc.zip<br /> * _**_@param _**_unzipDir 所有解压出来的文件的存放路径,例如G:/bde<br /> */<br /> _public static List<String> unzip(String zipPath, String unzipDir) {<br /> if (zipPath == null || "".equals(zipPath) || unzipDir == null || "".equals(unzipDir)) {<br /> return null;<br /> }<br /> File unzipDirFile = new File(unzipDir);<br /> if (!unzipDirFile.exists() && !unzipDirFile.isDirectory()) {<br /> unzipDirFile.mkdir();<br /> }<br /> File file = new File(zipPath);<br /> if (file == null || !file.exists() || !file.isFile() || !file.canRead()) {<br /> return null;<br /> }<br /> ZipFile zipFile = null;<br /> try {<br /> List<String> list = new ArrayList<String>();<br /> zipFile = new ZipFile(zipPath);<br /> System._out_.println(zipFile.getName());<br /> Enumeration<?> enumeration = zipFile.entries();<br /> while (enumeration.hasMoreElements()) {<br /> ZipEntry entry = (ZipEntry) enumeration.nextElement();<br /> String filepath = unzipDir + "/" + entry.getName();<br /> boolean result = _writeToFile_(zipFile, entry, new File(filepath));<br /> if (result == false) {<br /> return null;<br /> }<br /> list.add(filepath);<br /> }<br /> zipFile.close();<br /> return list;<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> _logger_.error("文件解压异常:", e);<br /> } finally {<br /> try {<br /> if (null != zipFile) {<br /> zipFile.close();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> return null;<br /> }
    2. private static boolean writeToFile(ZipFile zip, ZipEntry entry, File toFile) {<br /> if (zip == null || entry == null || toFile == null) {<br /> return false;<br /> }<br /> InputStream is = null;<br /> FileOutputStream fos = null;<br /> try {<br /> if (!entry.isDirectory()) {<br /> if (!toFile.exists()) {<br /> toFile.getParentFile().mkdirs();<br /> toFile.createNewFile();<br /> }<br /> is = zip.getInputStream(entry);<br /> fos = new FileOutputStream(toFile);<br /> int len = 0;<br /> long size = 0;<br /> byte[] buffer = new byte[1024];<br /> while ((len = is.read(buffer)) != -1) {<br /> fos.write(buffer, 0, len);<br /> size += len;<br /> }<br /> fos.flush();<br /> if (entry.getSize() == size) {<br /> return true;<br /> }<br /> } else {<br /> if (!toFile.exists()) {<br /> return toFile.mkdirs();<br /> }<br /> return true;<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> } finally {<br /> try {<br /> if (is != null) {<br /> is.close();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> try {<br /> if (fos != null) {<br /> fos.close();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> return false;<br /> }
    3. _/**<br /> * _**_@param _**_srcDir 压缩文件夹路径<br /> * _**_@param _**_basedir 压缩文件里的压缩路径(默认为“”)<br /> * _**_@param _**_KeepDirStructure 是否保留原来的目录结构,<br /> * true:保留目录结构;<br /> * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)<br /> * _**_@throws _**_RuntimeException 压缩失败会抛出运行时异常<br /> */<br /> _public static void toZip(List<String> srcDir, String basedir, String outDir,<br /> boolean KeepDirStructure) throws RuntimeException, Exception {<br /> OutputStream out = new FileOutputStream(new File(outDir));<br /> if (!StringUtils._isNotBlank_(basedir)) {<br /> basedir = "";<br /> }<br /> long start = System._currentTimeMillis_();<br /> ZipOutputStream zos = null;<br /> try {<br /> zos = new ZipOutputStream(out);
    4. List<File> sourceFileList = new ArrayList<File>();<br /> for (String dir : srcDir) {<br /> File sourceFile = new File(dir);<br /> sourceFileList.add(sourceFile);<br /> }<br /> _compress_(sourceFileList, basedir, zos, KeepDirStructure);<br /> long end = System._currentTimeMillis_();<br /> _logger_.info("压缩完成,耗时:" + (end - start) + " ms,压缩路径:" + outDir);<br /> } catch (Exception e) {<br /> throw new RuntimeException("zip error from ZipUtils", e);<br /> } finally {<br /> try {<br /> if (zos != null) {<br /> zos.close();<br /> }<br /> } catch (IOException e) {<br /> e.printStackTrace();<br /> }
    5. }
    6. }
    7. _/**<br /> * 递归压缩方法<br /> *<br /> * _**_@param _**_sourceFile 源文件<br /> * _**_@param _**_zos zip输出流<br /> * _**_@param _**_name 压缩后的名称<br /> * _**_@param _**_KeepDirStructure 是否保留原来的目录结构,<br /> * true:保留目录结构;<br /> * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)<br /> * _**_@throws _**_Exception<br /> */<br /> _private static void compress(File sourceFile, ZipOutputStream zos,<br /> String name, boolean KeepDirStructure) throws Exception {<br /> byte[] buf = new byte[_BUFFER_SIZE_];<br /> if (sourceFile.isFile()) {<br /> if (zos != null) {<br /> zos.putNextEntry(new ZipEntry(name));<br /> }<br /> int len;<br /> FileInputStream in = null;<br /> try {<br /> in = new FileInputStream(sourceFile);<br /> while ((len = in.read(buf)) != -1) {<br /> zos.write(buf, 0, len);<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> } finally {<br /> try {<br /> if (zos != null) {<br /> zos.close();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> try {<br /> if (in != null) {<br /> in.close();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> } else {<br /> File[] listFiles = sourceFile.listFiles();<br /> if (listFiles == null || listFiles.length == 0) {<br /> if (KeepDirStructure) {<br /> zos.putNextEntry(new ZipEntry(name + "/"));<br /> zos.closeEntry();<br /> }
    8. } else {<br /> for (File file : listFiles) {<br /> if (KeepDirStructure) {<br /> _compress_(file, zos, name + "/" + file.getName(),<br /> KeepDirStructure);<br /> } else {<br /> _compress_(file, zos, file.getName(), KeepDirStructure);<br /> }
    9. }<br /> }<br /> }<br /> }
    10. private static void compress(List<File> sourceFileList, String basedir,<br /> ZipOutputStream zos, boolean KeepDirStructure) throws Exception {<br /> byte[] buf = new byte[_BUFFER_SIZE_];<br /> for (File sourceFile : sourceFileList) {<br /> String name = sourceFile.getName();<br /> if (sourceFile.isFile()) {<br /> if (zos != null) {<br /> zos.putNextEntry(new ZipEntry(basedir + name));<br /> }<br /> int len;<br /> FileInputStream in = null;<br /> try {<br /> in = new FileInputStream(sourceFile);<br /> while ((len = in.read(buf)) != -1) {<br /> zos.write(buf, 0, len);<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> } finally {<br /> try {<br /> if (zos != null) {<br /> zos.closeEntry();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> try {<br /> if (in != null) {<br /> in.close();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> } else {<br /> File[] listFiles = sourceFile.listFiles();<br /> if (listFiles == null || listFiles.length == 0) {<br /> if (KeepDirStructure) {<br /> if (zos != null) {<br /> zos.putNextEntry(new ZipEntry(name + "/"));<br /> }<br /> try {<br /> if (zos != null) {<br /> zos.closeEntry();<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }
    11. } else {<br /> for (File file : listFiles) {<br /> if (KeepDirStructure) {<br /> _compress_(file, zos, name + "/" + file.getName(),<br /> KeepDirStructure);<br /> } else {<br /> _compress_(file, zos, file.getName(),<br /> KeepDirStructure);<br /> }
    12. }<br /> }<br /> }<br /> }<br /> }
    13. public static void main(String[] args) throws IOException {<br /> _unzip_("D:\\tmp\\11.zip", "D:\\tmp");<br /> }

    }