123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package org.jeecg.modules.demo.onemap.utils;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import com.github.junrar.Archive;
- import com.github.junrar.rarfile.FileHeader;
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- /**
- * @Description: TODO
- * @author: qiudongbao
- * @date: 2021年08月03日 13:46
- */
- /**
- * zip和rar解压缩工具类
- *
- * @author lenovo
- */
- public class ZipAndRarTools {
- /**
- * 解压rar
- *
- * @param sourceRarPath 需要解压的rar文件全路径
- * @param destDirPath 需要解压到的文件目录
- * @throws Exception
- */
- public static void unrar(String sourceRarPath, String destDirPath) throws Exception {
- File sourceRar = new File(sourceRarPath);
- File destDir = new File(destDirPath);
- Archive archive = null;
- FileOutputStream fos = null;
- System.out.println("Starting 开始解压...");
- try {
- archive = new Archive(sourceRar);
- FileHeader fh = archive.nextFileHeader();
- int count = 0;
- File destFileName = null;
- while (fh != null) {
- System.out.println((++count) + ") " + fh.getFileNameString());
- String compressFileName = fh.getFileNameString().trim();
- destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);
- if (fh.isDirectory()) {
- if (!destFileName.exists()) {
- destFileName.mkdirs();
- }
- fh = archive.nextFileHeader();
- continue;
- }
- if (!destFileName.getParentFile().exists()) {
- destFileName.getParentFile().mkdirs();
- }
- fos = new FileOutputStream(destFileName);
- archive.extractFile(fh, fos);
- fos.close();
- fos = null;
- fh = archive.nextFileHeader();
- }
- archive.close();
- archive = null;
- System.out.println("Finished 解压完成!");
- } catch (Exception e) {
- throw e;
- } finally {
- if (fos != null) {
- try {
- fos.close();
- fos = null;
- } catch (Exception e) {
- }
- }
- if (archive != null) {
- try {
- archive.close();
- archive = null;
- } catch (Exception e) {
- }
- }
- }
- }
- /**
- * 解压Zip文件
- *
- * @param zipFileName 需要解压缩的文件位置
- * @param descFileName 将文件解压到某个路径
- * @throws IOException
- */
- public static void unZip(String zipFileName, String descFileName) throws IOException {
- System.out.println("文件解压开始...");
- System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); //防止文件名中有中文时出错
- System.out.println(System.getProperty("sun.zip.encoding")); //ZIP编码方式
- System.out.println(System.getProperty("sun.jnu.encoding")); //当前文件编码方式
- System.out.println(System.getProperty("file.encoding")); //这个是当前文件内容编码方式
- String descFileNames = descFileName;
- if (!descFileNames.endsWith(File.separator)) {
- descFileNames = descFileNames + File.separator;
- }
- try {
- ZipFile zipFile = new ZipFile(zipFileName, "UTF-8");
- ZipEntry entry = null;
- String entryName = null;
- String descFileDir = null;
- byte[] buf = new byte[4096];
- int readByte = 0;
- @SuppressWarnings("rawtypes")
- Enumeration enums = zipFile.getEntries();
- while (enums.hasMoreElements()) {
- entry = (ZipEntry) enums.nextElement();
- entryName = entry.getName();
- descFileDir = descFileNames + entryName;
- if (entry.isDirectory()) {
- new File(descFileDir).mkdir();
- continue;
- } else {
- new File(descFileDir).getParentFile().mkdir();
- }
- File file = new File(descFileDir);
- OutputStream os = new FileOutputStream(file);
- InputStream is = zipFile.getInputStream(entry);
- while ((readByte = is.read(buf)) != -1) {
- os.write(buf, 0, readByte);
- }
- os.close();
- is.close();
- }
- zipFile.close();
- System.out.println("文件解压成功!");
- } catch (Exception e) {
- System.out.println("文件解压失败!");
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws Exception {
- //ZipAndRarTools.unrar(newFile("D:\\存放资料的压缩包\\员工材料.rar"),newFile("D:\\存放资料的非压缩包\\"));
- ZipAndRarTools.unZip("E:\\upFiles\\工程_1627962978919.zip", "e:\\upFiles\\工程_1627962978919");
- // ZipAndRarTools.unrar("D:\\rarTest\\rar压缩包.rar", "D:\\rarTest");
- }
- }
|