java文件解压的几种方法


1.java代码的方式解压

/**
     * 解压目录下的zip文件到指定目录
     *
     * @param dataSetInfo
     * @throws Exception
     */
    public void unzipFile2(String destDirPath,String path) throws Exception {
        File resourceFile = new File(path);
        if (!resourceFile.exists()) {
            throw new Exception("源目标路径:[" + path + "] 不存在...");
        }
        File[] resourceFiles = resourceFile.listFiles();
        for (File file : resourceFiles) {
            if(file.getName().endsWith("zip")){
                String zipPath = path+File.separator+file.getName();
                File srcFile = new File(zipPath);
                // 开始解压
                ZipFile zipFile = null;
                try {
                    zipFile = new ZipFile(srcFile, Charset.forName("GBK"));
                    Enumeration<?> entries = zipFile.entries();
                    while (entries.hasMoreElements()) {
                        ZipEntry entry = (ZipEntry) entries.nextElement();
                        // 如果是文件夹,就创建个文件夹
                        if (entry.isDirectory()) {
                            String dirPath = destDirPath + "/" + entry.getName();
                            File dir = new File(dirPath);
                            dir.mkdirs();
                        } else {
                            // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                            File targetFile = new File(destDirPath + "/" + entry.getName());
                            // 保证这个文件的父文件夹必须要存在
                            if (!targetFile.getParentFile().exists()) {
                                targetFile.getParentFile().mkdirs();
                            }
                            targetFile.createNewFile();
                            // 将压缩文件内容写入到这个文件中
                            InputStream is = zipFile.getInputStream(entry);
                            FileOutputStream fos = new FileOutputStream(targetFile);
                            int len;
                            int BUFFER_SIZE = 1024;
                            byte[] buf = new byte[BUFFER_SIZE];
                            while ((len = is.read(buf)) != -1) {
                                fos.write(buf, 0, len);
                            }
                            // 关流顺序,先打开的后关闭
                            fos.close();
                            is.close();
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException("unzip error from ZipUtils", e);
                } finally {
                    if (zipFile != null) {
                        try {
                            zipFile.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                System.out.println("destDirPath======"+destDirPath);
            }
        }
    }

2.使用linux命令的方式解压(适合解压较大的文件)

/**
     * linux命令解压zip文件
     *
     * @param dataSetInfo
     * @throws Exception
     */
    public String unzipFileByLinuxCommand(String destDirPathFather,String path) throws Exception {
        File resourceFile = new File(path);
        if (!resourceFile.exists()) {
            throw new Exception("源目标路径:[" + path + "] 不存在...");
        }
        File dir = new File(destDirPathFather);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File[] resourceFiles = resourceFile.listFiles();
        for (File file : resourceFiles) {
            if(file.getName().endsWith("zip")){
                String destDirPath = destDirPathFather+File.separator+file.getName().substring(0,file.getName().lastIndexOf("."));
                String zipPath = path+File.separator+file.getName();
          String cmd = "cd " + path + " && unzip -q -O gbk " +  file.getName() + " -d " + destDirPath;
                exeCmd(cmd, true);
                System.out.println("destDirPath======"+destDirPath);
            }
        }
        return destDirPathFather;
    }

public String exeCmd(String commandStr, boolean printLog) {
BufferedReader br = null;
try {
if (printLog) log.info("commandStr: {}",commandStr);
Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c", commandStr});
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
p.waitFor();
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
log.info("=========pull: {}", line);
sb.append(line + "\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "";
}