1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import com.github.junrar.Archive; import com.github.junrar.rarfile.FileHeader;
public class ZipUtil {
public static synchronized void unrar(String rarFileName, String extPlace) throws Exception { unRarFile(rarFileName, extPlace); }
public static synchronized void unzip(String zipFileName, String extPlace) throws Exception { unZipFiles(zipFileName, extPlace); }
public static boolean unZipFiles(String zipFileName, String extPlace) throws Exception { System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); try { (new File(extPlace)).mkdirs(); File f = new File(zipFileName); ZipFile zipFile = new ZipFile(zipFileName, "GBK"); if ((!f.exists()) && (f.length() <= 0)) { throw new Exception("要解压的文件不存在!"); } String strPath, gbkPath, strtemp; File tempFile = new File(extPlace); strPath = tempFile.getAbsolutePath(); Enumeration<?> e = zipFile.getEntries(); while (e.hasMoreElements()) { 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(); } } return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public static void unRarFile(String srcRarPath, String dstDirectoryPath) { if (!srcRarPath.toLowerCase().endsWith(".rar")) { System.out.println("非rar文件!"); return; } File dstDiretory = new File(dstDirectoryPath); if (!dstDiretory.exists()) { dstDiretory.mkdirs(); } Archive a = null; try { a = new Archive(new File(srcRarPath)); if (a != null) { FileHeader fh = a.nextFileHeader(); while (fh != null) { String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW(); if (fh.isDirectory()) { File fol = new File(dstDirectoryPath + File.separator + fileName); fol.mkdirs(); } else { File out = new File(dstDirectoryPath + File.separator + fileName.trim()); try { if (!out.exists()) { if (!out.getParentFile().exists()) { out.getParentFile().mkdirs(); } out.createNewFile(); } FileOutputStream os = new FileOutputStream(out); a.extractFile(fh, os); os.close(); } catch (Exception ex) { ex.printStackTrace(); } } fh = a.nextFileHeader(); } a.close(); } } catch (Exception e) { e.printStackTrace(); } } }
|