首頁  >  文章  >  Java  >  Java實作解壓縮與壓縮資料夾的案例

Java實作解壓縮與壓縮資料夾的案例

黄舟
黄舟原創
2017-10-16 10:09:411582瀏覽

這篇文章主要介紹了java 解壓縮與壓縮文件夾的實例詳解的相關資料,希望透過本文能幫助到大家,讓大家實現這樣的功能,掌握這樣的方法,需要的朋友可以參考下

 java 解壓縮與壓縮資料夾的實例詳解

注意:JDK7支援設定編碼設定編碼格式zipFile,zipInputStream,zipOutputStream都增加了編碼格式,如果是jdk1.6需要其他的套件輔助

下面為自帶jdk壓縮資料夾程式碼:


public void dozip(String srcfile, String zipfile) throws IOException {
  String temp = "";
  File src = new File(srcfile);
  File zipFile=new File(zipfile);
  //判断要压缩的文件存不存在
  if (!src.exists()) {
    System.err.println("要压缩的文件不存在!");
    System.exit(1);
  }
  //如果说压缩路径不存在,则创建
  if (!zipFile.getParentFile().exists()) {
    zipFile.getParentFile().mkdirs();
//   System.out.println("创建ok");
  }
  // 封装压缩的路径
  BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(zipfile));
  //这里可以加入校验
//CheckedOutputStream cos = new CheckedOutputStream(bos,new CRC32());  
  //还可以设置压缩格式,默认UTF-8
  Charset charset = Charset.forName("GBK"); 
  ZipOutputStream zos = new ZipOutputStream(bos,charset);
  zip(src, zos, temp);
  //关闭流
  zos.flush();
  zos.close();
  System.out.println("压缩完成!");
  System.out.println("压缩文件的位置是:" + zipfile);
// System.out.println("检验和:"+cos.getChecksum().getValue());
  }

 private void zip(File file, ZipOutputStream zos, String temp)
    throws IOException {
  // 如果不加"/"将会作为文件处理,空文件夹不需要读写操作
  if (file.isDirectory()) {
    String str = temp + file.getName() + "/";
    zos.putNextEntry(new ZipEntry(str));
    File[] files = file.listFiles();
    for (File file2 : files) {
    zip(file2, zos, str);
    }
  } else {
    // System.out.println("当前文件的父路径:"+temp);
    ZipFile(file, zos, temp);
  }
  }

  private void ZipFile(File srcfile, ZipOutputStream zos, String temp)
    throws IOException {
  // 默认的等级压缩-1
  // zos.setLevel(xxx);
  // 封装待压缩文件
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    srcfile));
  zos.putNextEntry(new ZipEntry(temp + srcfile.getName()));

  byte buf[] = new byte[1024];
  int len;
  while ((len = bis.read(buf)) != -1) {
    zos.write(buf, 0, len);
  }
  //按标准需要关闭当前条目,不写也行
  zos.closeEntry();
  bis.close();
  }

下面為解壓縮:

這裡先說一下好壓的解壓縮規則:

1. 如果解壓縮到與壓縮檔案同名的資料夾,則直接解壓縮

如果自訂了其他資料夾xxx,則先建立xxx,再放入解壓縮後的資料夾

#2.好壓壓縮的時候,是採用GBK格式的,所以在解壓縮的時候,為了統一,採用GBK解壓縮另外再說一下WINRAR,因為RAR壓縮是申請了專利(商業軟體),所以RAR壓縮演算法是不公開的,但是解壓縮演算法是有的,其壓縮預設也是GBK格式的;
經過測試,發現,不管壓縮的時候採用UTF-8還是GBK,解壓縮的時候用GBK都可以正確解壓縮! (具體原因還不清楚)

本java程式是直接解壓縮到資料夾的,預設解壓縮到與壓縮檔案同路徑

如果解壓縮編碼有問題,則報錯: java.lang.IllegalArgumentException: MALFORMED

如果壓縮檔案有密碼:則報錯:java.util.zip.ZipException: encrypted ZIP entry not supporte

##
//方法1:
public void unZip(String zipfile) throws IOException {
  //检查是否是zip文件,并判断文件是否存在
  checkFileName(zipfile);
  long startTime = System.currentTimeMillis();
  File zfile=new File(zipfile);
  //获取待解压文件的父路径
  String Parent=zfile.getParent()+"/";
  FileInputStream fis=new FileInputStream(zfile);
  Charset charset = Charset.forName("GBK");//默认UTF-8
// CheckedInputStream cis = new CheckedInputStream(fis,new CRC32());
  ZipInputStream zis = new ZipInputStream(fis,charset);// 输入源zip路径
  ZipEntry entry=null;
  BufferedOutputStream bos=null;
  while ((entry=zis.getNextEntry())!=null) {
    if (entry.isDirectory()) {
    File filePath=new File(Parent+entry.getName());
    //如果目录不存在,则创建
    if (!filePath.exists()) {
      filePath.mkdirs();
    }
    }else{
    FileOutputStream fos=new FileOutputStream(Parent+entry.getName());
    bos=new BufferedOutputStream(fos);
    byte buf[] = new byte[1024];
    int len;
    while ((len = zis.read(buf)) != -1) {
      bos.write(buf, 0, len);
    }
    zis.closeEntry();
    //关闭的时候会刷新
    bos.close();
    }
  }
  zis.close();
  long endTime = System.currentTimeMillis();
  System.out.println("解压完成!所需时间为:"+(endTime-startTime)+"ms");
// System.out.println("校验和:"+cis.getChecksum().getValue());
  }

  private void checkFileName(String name) {
  //文件是否存在
  if (!new File(name).exists()) {
    System.err.println("要解压的文件不存在!");
    System.exit(1);
  }
  // 判断是否是zip文件
  int index = name.lastIndexOf(".");
  String str=name.substring(index+1);
  if (!"zip".equalsIgnoreCase(str)) {
    System.err.println("不是zip文件,无法解压!");
    System.exit(1);
  } 
    }

方法2:

利用zipFile解壓縮,方法跟ZipInputStream類似,都是連續取到Entry,然後再用Entry判斷,聽說zipFile內建了緩衝流,所以對於同一個檔案解壓縮多次效率比ZipInputStream高些

 public void dozip(String zipfile) throws IOException {
  checkFileName(zipfile);
  long startTime = System.currentTimeMillis();
  // 获取待解压文件的父路径
  File zfile = new File(zipfile);
  String Parent = zfile.getParent() + "/";
  // 设置,默认是UTF-8
  Charset charset = Charset.forName("GBK");
  ZipFile zip = new ZipFile(zipfile, charset);
  ZipEntry entry = null;
  //封装解压后的路径
  BufferedOutputStream bos=null;
  //封装待解压文件路径
  BufferedInputStream bis=null;
  Enumeration<ZipEntry> enums = (Enumeration<ZipEntry>) zip.entries();
  while (enums.hasMoreElements()) {
    entry = enums.nextElement();
    if (entry.isDirectory()) {
    File filePath = new File(Parent + entry.getName());
    // 如果目录不存在,则创建
    if (!filePath.exists()) {
      filePath.mkdirs();
    }
    }else{
    bos=new BufferedOutputStream(new FileOutputStream(Parent + entry.getName()));
    //获取条目流
bis =new BufferedInputStream(zip.getInputStream(entry));
    byte buf[] = new byte[1024];
    int len;
    while ((len = bis.read(buf)) != -1) {
      bos.write(buf, 0, len);
    }

    bos.close();
    }
  }
  bis.close();
  zip.close();
  System.out.println("解压后的路径是:"+Parent);
  long endTime = System.currentTimeMillis();
  System.out.println("解压成功,所需时间为:"+(endTime-startTime)+"ms");
  }

  private void checkFileName(String name) {
  // 文件是否存在
  if (!new File(name).exists()) {
    System.err.println("要解压的文件不存在!");
    System.exit(1);
  }
  // 判断是否是zip文件
  int index = name.lastIndexOf(".");
  String str = name.substring(index + 1);
  if (!"zip".equalsIgnoreCase(str)) {
    System.err.println("不是zip文件,无法解压!");
    System.exit(1);
  }
  }

以上是Java實作解壓縮與壓縮資料夾的案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn