通过java实现解压zip,rar的代码。

原创不易,转载请注明出处:java解压zip,rar的代码

Java代码  收藏代码
  1. package com.zuidaima.util.unzip;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8. import java.net.URL;  
  9. import java.util.zip.ZipEntry;  
  10. import java.util.zip.ZipInputStream;  
  11.   
  12. import com.github.junrar.Archive;  
  13. import com.github.junrar.exception.RarException;  
  14. import com.github.junrar.rarfile.FileHeader;  
  15.   
  16. public class Main {  
  17.   
  18.     public void extractZipFiles() {  
  19.         String root = "c:/javazip/";  
  20.         URL url = Main.class.getResource("/aa.zip");  
  21.         String filename = url.getFile();  
  22.         try {  
  23.             // destination folder to extract the contents  
  24.             byte[] buf = new byte[1024];  
  25.             ZipInputStream zipinputstream = null;  
  26.             ZipEntry zipentry;  
  27.             zipinputstream = new ZipInputStream(new FileInputStream(filename));  
  28.             zipentry = zipinputstream.getNextEntry();  
  29.             while (zipentry != null) {  
  30.   
  31.                 // for each entry to be extracted  
  32.                 String entryName = zipentry.getName();  
  33.   
  34.                 System.out.println(entryName);  
  35.   
  36.                 int n;  
  37.                 FileOutputStream fileoutputstream;  
  38.                 File newFile = new File(entryName);  
  39.   
  40.                 String directory = newFile.getParent();  
  41.   
  42.                 // to creating the parent directories  
  43.                 if (directory == null) {  
  44.                     if (newFile.isDirectory()) {  
  45.                         break;  
  46.                     }  
  47.                 } else {  
  48.                     new File(root + directory).mkdirs();  
  49.                 }  
  50.   
  51.                 if (!zipentry.isDirectory()) {  
  52.                     System.out.println("File to be extracted....." + entryName);  
  53.                     fileoutputstream = new FileOutputStream(root + entryName);  
  54.                     while ((n = zipinputstream.read(buf, 01024)) > -1) {  
  55.                         fileoutputstream.write(buf, 0, n);  
  56.                     }  
  57.                     fileoutputstream.close();  
  58.                 }  
  59.   
  60.                 zipinputstream.closeEntry();  
  61.                 zipentry = zipinputstream.getNextEntry();  
  62.             }  
  63.             zipinputstream.close();  
  64.         } catch (Exception e) {  
  65.             e.printStackTrace();  
  66.         }  
  67.     }  
  68.   
  69.     public void extractRarFiles() {  
  70.         URL url = Main.class.getResource("/bb.rar");  
  71.         String filename = url.getFile();  
  72.         File archive = new File(filename);  
  73.         File destination = new File("c:/javazip/");  
  74.   
  75.         Archive arch = null;  
  76.         try {  
  77.             arch = new Archive(archive);  
  78.         } catch (RarException e) {  
  79.             e.printStackTrace();  
  80.         } catch (IOException e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.         if (arch != null) {  
  84.             if (arch.isEncrypted()) {  
  85.                 System.out.println("archive is encrypted cannot extreact");  
  86.                 return;  
  87.             }  
  88.             FileHeader fh = null;  
  89.             while (true) {  
  90.                 fh = arch.nextFileHeader();  
  91.                 if (fh == null) {  
  92.                     break;  
  93.                 }  
  94.                 if (fh.isEncrypted()) {  
  95.                     System.out.println("file is encrypted cannot extract: "  
  96.                             + fh.getFileNameString());  
  97.                     continue;  
  98.                 }  
  99.                 System.out.println("extracting: " + fh.getFileNameString());  
  100.                 try {  
  101.                     if (fh.isDirectory()) {  
  102.                         createDirectory(fh, destination);  
  103.                     } else {  
  104.                         File f = createFile(fh, destination);  
  105.                         OutputStream stream = new FileOutputStream(f);  
  106.                         arch.extractFile(fh, stream);  
  107.                         stream.close();  
  108.                     }  
  109.                 } catch (Exception e) {  
  110.                     e.printStackTrace();  
  111.                 }  
  112.             }  
  113.         }  
  114.     }  
  115.   
  116.     private File createFile(FileHeader fh, File destination) {  
  117.         File f = null;  
  118.         String name = null;  
  119.         if (fh.isFileHeader() && fh.isUnicode()) {  
  120.             name = fh.getFileNameW();  
  121.         } else {  
  122.             name = fh.getFileNameString();  
  123.         }  
  124.         f = new File(destination, name);  
  125.         if (!f.exists()) {  
  126.             try {  
  127.                 f = makeFile(destination, name);  
  128.             } catch (IOException e) {  
  129.                 e.printStackTrace();  
  130.             }  
  131.         }  
  132.         return f;  
  133.     }  
  134.   
  135.     private File makeFile(File destination, String name) throws IOException {  
  136.         String[] dirs = name.split("\\\\");  
  137.         if (dirs == null) {  
  138.             return null;  
  139.         }  
  140.         String path = "";  
  141.         int size = dirs.length;  
  142.         if (size == 1) {  
  143.             return new File(destination, name);  
  144.         } else if (size > 1) {  
  145.             for (int i = 0; i < dirs.length - 1; i++) {  
  146.                 path = path + File.separator + dirs[i];  
  147.                 new File(destination, path).mkdir();  
  148.             }  
  149.             path = path + File.separator + dirs[dirs.length - 1];  
  150.             File f = new File(destination, path);  
  151.             f.createNewFile();  
  152.             return f;  
  153.         } else {  
  154.             return null;  
  155.         }  
  156.     }  
  157.   
  158.     private void createDirectory(FileHeader fh, File destination) {  
  159.         File f = null;  
  160.         if (fh.isDirectory() && fh.isUnicode()) {  
  161.             f = new File(destination, fh.getFileNameW());  
  162.             if (!f.exists()) {  
  163.                 makeDirectory(destination, fh.getFileNameW());  
  164.             }  
  165.         } else if (fh.isDirectory() && !fh.isUnicode()) {  
  166.             f = new File(destination, fh.getFileNameString());  
  167.             if (!f.exists()) {  
  168.                 makeDirectory(destination, fh.getFileNameString());  
  169.             }  
  170.         }  
  171.     }  
  172.   
  173.     private void makeDirectory(File destination, String fileName) {  
  174.         String[] dirs = fileName.split("\\\\");  
  175.         if (dirs == null) {  
  176.             return;  
  177.         }  
  178.         String path = "";  
  179.         for (String dir : dirs) {  
  180.             path = path + File.separator + dir;  
  181.             new File(destination, path).mkdir();  
  182.         }  
  183.     }  
  184.   
  185.     public static void main(String[] args) {  
  186.         Main main = new Main();  
  187.         // extract zip  
  188.         main.extractZipFiles();  
  189.         // extract rar  
  190.         main.extractRarFiles();  
  191.     }  
  192. }  
  193.   
  194.                       

 代码下载地址:http://www.zuidaima.com/share/1550463229430784.htm

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()