[Java] java压缩与解压(Java.util.zip) →→→→→进入此内容的聊天室

来自 , 2019-08-10, 写在 Java, 查看 107 次.
URL http://www.code666.cn/view/7a53928f
  1.  
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipException;
  12. import java.util.zip.ZipFile;
  13. import java.util.zip.ZipInputStream;
  14. import java.util.zip.ZipOutputStream;
  15.  
  16. /**
  17.  * 文件解压缩
  18.  *
  19.  * @author Tony
  20.  *
  21.  */
  22. public final class FileToZip {
  23.  
  24.     FileToZip() {
  25.  
  26.     }
  27.  
  28.     /**
  29.      * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。
  30.      *
  31.      * @param sourceFilePath
  32.      *            待压缩的文件路径
  33.      * @param zipFilePath
  34.      *            压缩后存放路径
  35.      * @param fileName
  36.      *            压缩后文件的名称
  37.      * @return flag
  38.      */
  39.     public static boolean fileToZip(String sourceFilePath, String zipFilePath,
  40.                                     String fileName) {
  41.         boolean flag = false;
  42.         File sourceFile = new File(sourceFilePath);
  43.         FileInputStream fis = null;
  44.         BufferedInputStream bis = null;
  45.         FileOutputStream fos = null;
  46.         ZipOutputStream zos = null;
  47.  
  48.         if (sourceFile.exists() == false) {
  49.             System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath
  50.                                + " 不存在. <<<<<<");
  51.         } else {
  52.             try {
  53.                 File zipFile = new File(zipFilePath + "/" + fileName + ".RAR");
  54.                 //                File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
  55.                 if (zipFile.exists()) {
  56.                     System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:"
  57.                                        + fileName + ".RAR" + " 打包文件. <<<<<<");
  58.                 } else {
  59.                     File[] sourceFiles = sourceFile.listFiles();
  60.                     if (null == sourceFiles || sourceFiles.length < 1) {
  61.                         System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath
  62.                                            + " 里面不存在文件,无需压缩. <<<<<<");
  63.                     } else {
  64.                         fos = new FileOutputStream(zipFile);
  65.                         zos = new ZipOutputStream(new BufferedOutputStream(fos));
  66.                         byte[] bufs = new byte[1024 * 10];
  67.                         for (int i = 0; i < sourceFiles.length; i++) {
  68.                             // 创建ZIP实体,并添加进压缩包
  69.                             ZipEntry zipEntry = new ZipEntry(sourceFiles[i]
  70.                                                              .getName());
  71.                             zos.putNextEntry(zipEntry);
  72.                             // 读取待压缩的文件并写进压缩包里
  73.                             fis = new FileInputStream(sourceFiles[i]);
  74.                             bis = new BufferedInputStream(fis, 1024 * 10);
  75.                             int read = 0;
  76.                             while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
  77.                                 zos.write(bufs, 0, read);
  78.                             }
  79.                         }
  80.                         flag = true;
  81.                     }
  82.                 }
  83.             } catch (FileNotFoundException e) {
  84.                 e.printStackTrace();
  85.                 throw new RuntimeException(e);
  86.             } catch (IOException e) {
  87.                 e.printStackTrace();
  88.                 throw new RuntimeException(e);
  89.             } finally {
  90.                 // 关闭流
  91.                 try {
  92.                     if (null != bis)
  93.                         bis.close();
  94.                     if (null != zos)
  95.                         zos.close();
  96.                 } catch (IOException e) {
  97.                     e.printStackTrace();
  98.                     throw new RuntimeException(e);
  99.                 }
  100.             }
  101.         }
  102.  
  103.         return flag;
  104.     }
  105.  
  106.     public static void unZip(String sourceFilePath, String unzipFilePath) {
  107.         File sourceFile = new File(sourceFilePath);
  108.         ZipFile zipFile = null;
  109.         ZipEntry zipEntry = null;
  110.         ZipInputStream zis = null;
  111.         FileOutputStream fos = null;
  112.         FileInputStream fis = null;
  113.  
  114.         if (sourceFile.exists() == false) {
  115.             System.out.println(">>>>>> 待解压的文件目录:" + sourceFilePath
  116.                                + " 不存在. <<<<<<");
  117.         } else {
  118.             try {
  119.                 System.out.println(">>>>>> 开始解压:" + sourceFilePath + " <<<<<<");
  120.                 zipFile = new ZipFile(sourceFile);
  121.                 zis = new ZipInputStream(new FileInputStream(sourceFile));
  122.                 while ((zipEntry = zis.getNextEntry()) != null) {
  123.                     String fileName = zipEntry.getName();
  124.                     File temp = new File(unzipFilePath + "\\" + fileName);
  125.                     System.out.println(fileName + ">>>>>>解压到" + unzipFilePath);
  126.                     if (!temp.getParentFile().exists()) {
  127.                         temp.getParentFile().mkdirs();
  128.                     }
  129.                     fos = new FileOutputStream(temp);
  130.                     InputStream is = zipFile.getInputStream(zipEntry);
  131.                     int len = 0;
  132.                     while ((len = is.read()) != -1) {
  133.                         fos.write(len);
  134.                     }
  135.                     is.close();
  136.                 }
  137.  
  138.             } catch (FileNotFoundException e) {
  139.                 // TODO Auto-generated catch block
  140.                 e.printStackTrace();
  141.             } catch (ZipException e) {
  142.                 // TODO Auto-generated catch block
  143.                 e.printStackTrace();
  144.             } catch (IOException e) {
  145.                 // TODO Auto-generated catch block
  146.                 e.printStackTrace();
  147.             } finally {
  148.                 // 关闭流
  149.                 try {
  150.                     if (null != fos)
  151.                         fos.close();
  152.                     if (null != fis)
  153.                         fis.close();
  154.                     if (null != zis)
  155.                         zis.close();
  156.                 } catch (IOException e) {
  157.                     e.printStackTrace();
  158.                     throw new RuntimeException(e);
  159.                 }
  160.             }
  161.         }
  162.  
  163.     }
  164.  
  165.     /**
  166.      * 将文件打包成ZIP压缩文件,main方法测试
  167.      *
  168.      * @param args
  169.      */
  170.     public static void main(String[] args) {
  171.         String sourceFilePath = "D:\\sss\\lp20120301.rar";
  172.         // String zipFilePath = "D:\\sss";
  173.         // String fileName = "lp20120301";
  174.         // boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath,
  175.         // fileName);
  176.         // if(flag) {
  177.         // System.out.println(">>>>>> 文件打包成功. <<<<<<");
  178.         // } else {
  179.         // System.out.println(">>>>>> 文件打包失败. <<<<<<");
  180.         // }
  181.         FileToZip.unZip(sourceFilePath, "E:\\sdf");
  182.     }
  183. }

回复 "java压缩与解压(Java.util.zip)"

这儿你可以回复上面这条便签

captcha