|
@@ -0,0 +1,423 @@
|
|
|
+package com.yc.education.util;
|
|
|
+
|
|
|
+import de.innosystec.unrar.Archive;
|
|
|
+import de.innosystec.unrar.rarfile.FileHeader;
|
|
|
+import org.apache.tools.zip.ZipEntry;
|
|
|
+import org.apache.tools.zip.ZipFile;
|
|
|
+import org.apache.tools.zip.ZipOutputStream;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 解压zip与Rar文件
|
|
|
+ * @author YaoShiHang
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ZipRarUtil {
|
|
|
+ private static final int BUFFEREDSIZE = 1024;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压zip格式的压缩文件到当前文件夹
|
|
|
+ * @param zipFileName
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static synchronized void unzipFile(String zipFileName) throws Exception {
|
|
|
+ try {
|
|
|
+ File f = new File(zipFileName);
|
|
|
+ ZipFile zipFile = new ZipFile(zipFileName);
|
|
|
+ if ((!f.exists()) && (f.length() <= 0)) {
|
|
|
+ throw new Exception("要解压的文件不存在!");
|
|
|
+ }
|
|
|
+ String strPath, gbkPath, strtemp;
|
|
|
+ File tempFile = new File(f.getParent());
|
|
|
+ strPath = tempFile.getAbsolutePath();
|
|
|
+ java.util.Enumeration e = zipFile.getEntries();
|
|
|
+ while (e.hasMoreElements()) {
|
|
|
+ org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
|
|
|
+ gbkPath = zipEnt.getName();
|
|
|
+ if (zipEnt.isDirectory()) {
|
|
|
+ strtemp = strPath + "/" + gbkPath;
|
|
|
+ File dir = new File(strtemp);
|
|
|
+ dir.mkdirs();
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ // 读写文件
|
|
|
+ InputStream is = zipFile.getInputStream(zipEnt);
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(is);
|
|
|
+ gbkPath = zipEnt.getName();
|
|
|
+ strtemp = strPath + "/" + gbkPath;
|
|
|
+
|
|
|
+ // 建目录
|
|
|
+ String strsubdir = gbkPath;
|
|
|
+ for (int i = 0; i < strsubdir.length(); i++) {
|
|
|
+ if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
|
|
|
+ String temp = strPath + "/" + strsubdir.substring(0, i);
|
|
|
+ File subdir = new File(temp);
|
|
|
+ if (!subdir.exists())
|
|
|
+ subdir.mkdir();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileOutputStream fos = new FileOutputStream(strtemp);
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
+ int c;
|
|
|
+ while ((c = bis.read()) != -1) {
|
|
|
+ bos.write((byte) c);
|
|
|
+ }
|
|
|
+ bos.close();
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压zip格式的压缩文件到指定位置
|
|
|
+ *
|
|
|
+ * @param zipFileName
|
|
|
+ * 压缩文件
|
|
|
+ * @param extPlace
|
|
|
+ * 解压目录
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static synchronized List<String> unzip(String zipFileName, String extPlace) throws Exception {
|
|
|
+ try {
|
|
|
+ (new File(extPlace)).mkdirs();
|
|
|
+ File f = new File(zipFileName);
|
|
|
+ List<String> nameList=new ArrayList<>();
|
|
|
+ ZipFile zipFile = new ZipFile(zipFileName);
|
|
|
+ if ((!f.exists()) && (f.length() <= 0)) {
|
|
|
+ throw new Exception("要解压的文件不存在!");
|
|
|
+ }
|
|
|
+ String strPath, gbkPath, strtemp;
|
|
|
+ File tempFile = new File(extPlace);
|
|
|
+ strPath = tempFile.getAbsolutePath();
|
|
|
+ java.util.Enumeration e = zipFile.getEntries();
|
|
|
+ while (e.hasMoreElements()) {
|
|
|
+ org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
|
|
|
+ gbkPath = zipEnt.getName();
|
|
|
+ System.err.println(gbkPath);
|
|
|
+ String [] name =gbkPath.split("/");
|
|
|
+
|
|
|
+ System.err.println(name.length);
|
|
|
+ for (String names : name) {
|
|
|
+ if(name.length==1){
|
|
|
+
|
|
|
+ nameList.add(names);
|
|
|
+
|
|
|
+ /*
|
|
|
+ String [] name2 =names.split("\\.");
|
|
|
+ if(name2.length>0){
|
|
|
+ nameList.add(name2[0]);
|
|
|
+ }else{
|
|
|
+ nameList.add(names);
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+ System.err.println(names);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (zipEnt.isDirectory()) {
|
|
|
+ strtemp = strPath + File.separator + gbkPath;
|
|
|
+ File dir = new File(strtemp);
|
|
|
+ dir.mkdirs();
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ // 读写文件
|
|
|
+ InputStream is = zipFile.getInputStream(zipEnt);
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(is);
|
|
|
+ gbkPath = zipEnt.getName();
|
|
|
+ strtemp = strPath + File.separator + gbkPath;
|
|
|
+
|
|
|
+
|
|
|
+ // 建目录
|
|
|
+ String strsubdir = gbkPath;
|
|
|
+ for (int i = 0; i < strsubdir.length(); i++) {
|
|
|
+ if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
|
|
|
+ String temp = strPath + File.separator + strsubdir.substring(0, i);
|
|
|
+ File subdir = new File(temp);
|
|
|
+ if (!subdir.exists())
|
|
|
+ subdir.mkdir();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileOutputStream fos = new FileOutputStream(strtemp);
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
+ int c;
|
|
|
+ while ((c = bis.read()) != -1) {
|
|
|
+ bos.write((byte) c);
|
|
|
+ }
|
|
|
+ bos.close();
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nameList;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压zip格式的压缩文件到指定位置
|
|
|
+ *
|
|
|
+ * @param zipFileName
|
|
|
+ * 压缩文件
|
|
|
+ * @param extPlace
|
|
|
+ * 解压目录
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static synchronized void unzip(String zipFileName, String extPlace, boolean whether) throws Exception {
|
|
|
+ try {
|
|
|
+ (new File(extPlace)).mkdirs();
|
|
|
+ File f = new File(zipFileName);
|
|
|
+ ZipFile zipFile = new ZipFile(zipFileName);
|
|
|
+ if ((!f.exists()) && (f.length() <= 0)) {
|
|
|
+ throw new Exception("要解压的文件不存在!");
|
|
|
+ }
|
|
|
+ String strPath, gbkPath, strtemp;
|
|
|
+ File tempFile = new File(extPlace);
|
|
|
+ strPath = tempFile.getAbsolutePath();
|
|
|
+ java.util.Enumeration e = zipFile.getEntries();
|
|
|
+ while (e.hasMoreElements()) {
|
|
|
+ org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
|
|
|
+ gbkPath = zipEnt.getName();
|
|
|
+ if (zipEnt.isDirectory()) {
|
|
|
+ strtemp = strPath + File.separator + gbkPath;
|
|
|
+ File dir = new File(strtemp);
|
|
|
+ dir.mkdirs();
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+
|
|
|
+ InputStream is = zipFile.getInputStream(zipEnt);
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(is);
|
|
|
+ gbkPath = zipEnt.getName();
|
|
|
+ strtemp = strPath + File.separator + gbkPath;
|
|
|
+
|
|
|
+ // 建目录
|
|
|
+ String strsubdir = gbkPath;
|
|
|
+ for (int i = 0; i < strsubdir.length(); i++) {
|
|
|
+ if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
|
|
|
+ String temp = strPath + File.separator + strsubdir.substring(0, i);
|
|
|
+ File subdir = new File(temp);
|
|
|
+ if (!subdir.exists())
|
|
|
+ subdir.mkdir();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FileOutputStream fos = new FileOutputStream(strtemp);
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
+ int c;
|
|
|
+ while ((c = bis.read()) != -1) {
|
|
|
+ bos.write((byte) c);
|
|
|
+ }
|
|
|
+ bos.close();
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip格式的压缩文件
|
|
|
+ *
|
|
|
+ * @param inputFilename
|
|
|
+ * 压缩的文件或文件夹及详细路径
|
|
|
+ * @param zipFilename
|
|
|
+ * 输出文件名称及详细路径
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static synchronized void zip(String inputFilename, String zipFilename) throws IOException {
|
|
|
+ zip(new File(inputFilename), zipFilename);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip格式的压缩文件
|
|
|
+ *
|
|
|
+ * @param inputFile
|
|
|
+ * 需压缩文件
|
|
|
+ * @param zipFilename
|
|
|
+ * 输出文件及详细路径
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public synchronized static void zip(File inputFile, String zipFilename) throws IOException {
|
|
|
+ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
|
|
|
+ try {
|
|
|
+ zip(inputFile, out, "");
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip格式的压缩文件
|
|
|
+ *
|
|
|
+ * @param inputFile
|
|
|
+ * 需压缩文件
|
|
|
+ * @param out
|
|
|
+ * 输出压缩文件
|
|
|
+ * @param base
|
|
|
+ * 结束标识
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private synchronized static void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
|
|
|
+ if (inputFile.isDirectory()) {
|
|
|
+ File[] inputFiles = inputFile.listFiles();
|
|
|
+ out.putNextEntry(new ZipEntry(base + "/"));
|
|
|
+ base = base.length() == 0 ? "" : base + "/";
|
|
|
+ for (int i = 0; i < inputFiles.length; i++) {
|
|
|
+ zip(inputFiles[i], out, base + inputFiles[i].getName());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (base.length() > 0) {
|
|
|
+ out.putNextEntry(new ZipEntry(base));
|
|
|
+ } else {
|
|
|
+ out.putNextEntry(new ZipEntry(inputFile.getName()));
|
|
|
+ }
|
|
|
+ FileInputStream in = new FileInputStream(inputFile);
|
|
|
+ try {
|
|
|
+ int c;
|
|
|
+ byte[] by = new byte[BUFFEREDSIZE];
|
|
|
+ while ((c = in.read(by)) != -1) {
|
|
|
+ out.write(by, 0, c);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行实例
|
|
|
+ *
|
|
|
+ * @param args
|
|
|
+ */
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ ZipRarUtil decompression = new ZipRarUtil();
|
|
|
+ //decompression.unzipFile("G:/tomcat.zip");
|
|
|
+ //decompression.unzip("G:/img.zip","G://img");
|
|
|
+ // decompression.zip("c:/Inetpub", "c:/Inetpub.zip");
|
|
|
+ //decompression.unrar("G:/1.rar", "G:/t/");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压 rar
|
|
|
+ * @param sourceRar
|
|
|
+ * @param destDir
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static List<String> unrar(String sourceRar, String destDir,int num) throws Exception {
|
|
|
+ Archive archive = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ List<String> nameList=new ArrayList<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ archive = new Archive(new File(sourceRar));
|
|
|
+ FileHeader fh = archive.nextFileHeader();
|
|
|
+ while (fh != null) {
|
|
|
+ if (!fh.isDirectory()) {
|
|
|
+ // 根据不同的操作系统拿到相应的 destDirName 和 destFileName
|
|
|
+ String compressFileName = fh.getFileNameString().trim();
|
|
|
+ String [] name =compressFileName.split("\\\\");
|
|
|
+ System.out.println(name.length);
|
|
|
+
|
|
|
+ if(!nameList.contains(name[0])){
|
|
|
+ nameList.add(name[0]);
|
|
|
+ }
|
|
|
+ /*if(name.length==2){
|
|
|
+ if(!nameList.contains(name[0])){
|
|
|
+ nameList.add(name[0]);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ String [] name2 =name[0].split("\\.");
|
|
|
+ nameList.add(name2[0]);
|
|
|
+ }*/
|
|
|
+ String destFileName = "";
|
|
|
+ String destDirName = "";
|
|
|
+ // 非windows系统
|
|
|
+ if (File.separator.equals("/")) {
|
|
|
+ destFileName = destDir + compressFileName.replaceAll("\\\\", "/");
|
|
|
+ destDirName = destFileName.substring(0, destFileName.lastIndexOf("/")); // windows系统
|
|
|
+ } else {
|
|
|
+ destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
|
|
|
+ String [] str=destFileName.split("\\\\");
|
|
|
+ if(str.length>num+1){
|
|
|
+ destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));;
|
|
|
+
|
|
|
+ }else{
|
|
|
+ destDirName = destDir;
|
|
|
+ }
|
|
|
+ System.out.println(str[str.length-1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ File dir = new File(destDirName);
|
|
|
+
|
|
|
+
|
|
|
+ if (!dir.exists() || !dir.isDirectory()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ } // 解压文件
|
|
|
+ fos = new FileOutputStream(new File(destFileName));
|
|
|
+ archive.extractFile(fh, fos);
|
|
|
+ fos.close();
|
|
|
+ fos = null;
|
|
|
+ }
|
|
|
+ fh = archive.nextFileHeader();
|
|
|
+ }
|
|
|
+ archive.close();
|
|
|
+ return nameList;
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ throw e;
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ if (fos != null) {
|
|
|
+ try {
|
|
|
+ fos.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (archive != null) {
|
|
|
+ try {
|
|
|
+ archive.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|