溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java如何解壓與壓縮文件夾

發(fā)布時間:2021-07-15 11:14:28 來源:億速云 閱讀:376 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)java如何解壓與壓縮文件夾,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

注意:JDK7支持設(shè)置編碼設(shè)置編碼格式 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);
  }
  //如果說壓縮路徑不存在,則創(chuàng)建
  if (!zipFile.getParentFile().exists()) {
    zipFile.getParentFile().mkdirs();
//   System.out.println("創(chuàng)建ok");
  }
  // 封裝壓縮的路徑
  BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(zipfile));
  //這里可以加入校驗
//CheckedOutputStream cos = new CheckedOutputStream(bos,new CRC32());  
  //還可以設(shè)置壓縮格式,默認UTF-8
  Charset charset = Charset.forName("GBK"); 
  ZipOutputStream zos = new ZipOutputStream(bos,charset);
  zip(src, zos, temp);
  //關(guān)閉流
  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);
  }
  //按標準需要關(guān)閉當前條目,不寫也行
  zos.closeEntry();
  bis.close();
  }

下面為解壓:

這里先說一下好壓的解壓規(guī)則:

1.如果解壓到與壓縮文件同名的文件夾,則直接解壓

如果自定義了其他文件夾xxx,則先創(chuàng)建xxx,再放入解壓后的文件夾

2.好壓壓縮的時候,是采用GBK格式的,所以在解壓的時候,為了統(tǒng)一,采用GBK解壓另外再說一下WINRAR,因為RAR壓縮是申請了專利(商業(yè)軟件),所以RAR壓縮算法是不公開的,但是解壓算法是有的,其壓縮默認也是GBK格式的;
經(jīng)過測試,發(fā)現(xiàn),不管壓縮的時候采用UTF-8還是GBK,解壓的時候用GBK都可以正確解壓?。ň唧w原因還不清楚)

本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());
    //如果目錄不存在,則創(chuàng)建
    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();
    //關(guān)閉的時候會刷新
    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類似,都是連續(xù)取到Entry,然后再用Entry判斷,聽說zipFile內(nèi)建了緩沖流,所以對于同一個文件解壓多次效率比ZipInputStream高些

 public void dozip(String zipfile) throws IOException {
  checkFileName(zipfile);
  long startTime = System.currentTimeMillis();
  // 獲取待解壓文件的父路徑
  File zfile = new File(zipfile);
  String Parent = zfile.getParent() + "/";
  // 設(shè)置,默認是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());
    // 如果目錄不存在,則創(chuàng)建
    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);
  }
  }

關(guān)于“java如何解壓與壓縮文件夾”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI