免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1377 | 回复: 0
打印 上一主题 下一主题

java解压zip,rar的代码 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-07-07 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-06 14:24 |只看该作者 |倒序浏览
通过java实现解压zip,rar的代码。
原文:http://www.zuidaima.com/share/1550463229430784.htm
[Java]代码
  1. package com.zuidaima.main;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.net.URL;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipInputStream;

  10. import com.github.junrar.Archive;
  11. import com.github.junrar.exception.RarException;
  12. import com.github.junrar.rarfile.FileHeader;

  13. /**
  14. *@author www.zuidaima.com
  15. **/
  16. public class Main {

  17.     public void extractZipFiles() {
  18.         String root = "c:/javazip/";
  19.         URL url = Main.class.getResource("/aa.zip");
  20.         String filename = url.getFile();
  21.         try {
  22.             // destination folder to extract the contents
  23.             byte[] buf = new byte[1024];
  24.             ZipInputStream zipinputstream = null;
  25.             ZipEntry zipentry;
  26.             zipinputstream = new ZipInputStream(new FileInputStream(filename));
  27.             zipentry = zipinputstream.getNextEntry();
  28.             while (zipentry != null) {

  29.                 // for each entry to be extracted
  30.                 String entryName = zipentry.getName();

  31.                 System.out.println(entryName);

  32.                 int n;
  33.                 FileOutputStream fileoutputstream;
  34.                 File newFile = new File(entryName);

  35.                 String directory = newFile.getParent();

  36.                 // to creating the parent directories
  37.                 if (directory == null) {
  38.                     if (newFile.isDirectory()) {
  39.                         break;
  40.                     }
  41.                 } else {
  42.                     new File(root + directory).mkdirs();
  43.                 }

  44.                 if (!zipentry.isDirectory()) {
  45.                     System.out.println("File to be extracted....." + entryName);
  46.                     fileoutputstream = new FileOutputStream(root + entryName);
  47.                     while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
  48.                         fileoutputstream.write(buf, 0, n);
  49.                     }
  50.                     fileoutputstream.close();
  51.                 }

  52.                 zipinputstream.closeEntry();
  53.                 zipentry = zipinputstream.getNextEntry();
  54.             }
  55.             zipinputstream.close();
  56.         } catch (Exception e) {
  57.             e.printStackTrace();
  58.         }
  59.     }

  60.     public void extractRarFiles() {
  61.         URL url = Main.class.getResource("/bb.rar");
  62.         String filename = url.getFile();
  63.         File archive = new File(filename);
  64.         File destination = new File("c:/javazip/");

  65.         Archive arch = null;
  66.         try {
  67.             arch = new Archive(archive);
  68.         } catch (RarException e) {
  69.             e.printStackTrace();
  70.         } catch (IOException e) {
  71.             e.printStackTrace();
  72.         }
  73.         if (arch != null) {
  74.             if (arch.isEncrypted()) {
  75.                 System.out.println("archive is encrypted cannot extreact");
  76.                 return;
  77.             }
  78.             FileHeader fh = null;
  79.             while (true) {
  80.                 fh = arch.nextFileHeader();
  81.                 if (fh == null) {
  82.                     break;
  83.                 }
  84.                 if (fh.isEncrypted()) {
  85.                     System.out.println("file is encrypted cannot extract: "
  86.                             + fh.getFileNameString());
  87.                     continue;
  88.                 }
  89.                 System.out.println("extracting: " + fh.getFileNameString());
  90.                 try {
  91.                     if (fh.isDirectory()) {
  92.                         createDirectory(fh, destination);
  93.                     } else {
  94.                         File f = createFile(fh, destination);
  95.                         OutputStream stream = new FileOutputStream(f);
  96.                         arch.extractFile(fh, stream);
  97.                         stream.close();
  98.                     }
  99.                 } catch (Exception e) {
  100.                     e.printStackTrace();
  101.                 }
  102.             }
  103.         }
  104.     }

  105.     private File createFile(FileHeader fh, File destination) {
  106.         File f = null;
  107.         String name = null;
  108.         if (fh.isFileHeader() && fh.isUnicode()) {
  109.             name = fh.getFileNameW();
  110.         } else {
  111.             name = fh.getFileNameString();
  112.         }
  113.         f = new File(destination, name);
  114.         if (!f.exists()) {
  115.             try {
  116.                 f = makeFile(destination, name);
  117.             } catch (IOException e) {
  118.                 e.printStackTrace();
  119.             }
  120.         }
  121.         return f;
  122.     }

  123.     private File makeFile(File destination, String name) throws IOException {
  124.         String[] dirs = name.split("\\\\");
  125.         if (dirs == null) {
  126.             return null;
  127.         }
  128.         String path = "";
  129.         int size = dirs.length;
  130.         if (size == 1) {
  131.             return new File(destination, name);
  132.         } else if (size > 1) {
  133.             for (int i = 0; i < dirs.length - 1; i++) {
  134.                 path = path + File.separator + dirs[i];
  135.                 new File(destination, path).mkdir();
  136.             }
  137.             path = path + File.separator + dirs[dirs.length - 1];
  138.             File f = new File(destination, path);
  139.             f.createNewFile();
  140.             return f;
  141.         } else {
  142.             return null;
  143.         }
  144.     }

  145.     private void createDirectory(FileHeader fh, File destination) {
  146.         File f = null;
  147.         if (fh.isDirectory() && fh.isUnicode()) {
  148.             f = new File(destination, fh.getFileNameW());
  149.             if (!f.exists()) {
  150.                 makeDirectory(destination, fh.getFileNameW());
  151.             }
  152.         } else if (fh.isDirectory() && !fh.isUnicode()) {
  153.             f = new File(destination, fh.getFileNameString());
  154.             if (!f.exists()) {
  155.                 makeDirectory(destination, fh.getFileNameString());
  156.             }
  157.         }
  158.     }

  159.     private void makeDirectory(File destination, String fileName) {
  160.         String[] dirs = fileName.split("\\\\");
  161.         if (dirs == null) {
  162.             return;
  163.         }
  164.         String path = "";
  165.         for (String dir : dirs) {
  166.             path = path + File.separator + dir;
  167.             new File(destination, path).mkdir();
  168.         }
  169.     }

  170.     public static void main(String[] args) {
  171.         Main main = new Main();
  172.         // extract zip
  173.         main.extractZipFiles();
  174.         // extract rar
  175.         main.extractRarFiles();
  176.     }
  177. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP