Java原始的壓縮和解壓

來源:互聯網
上載者:User

標籤:package   import   public   java   source   

package com.ahzc.test;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;public class Compress {/**   * 功能:把 sourceDir 目錄下的所有檔案進行 zip 格式的壓縮,儲存為指定 zip 檔案  * @param sourceDir 如果是目錄,eg:D:\\MyEclipse\\first\\testFile,則壓縮目錄下所有檔案;  *      如果是檔案,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,則只壓縮本檔案  * @param zipFile 最後壓縮的檔案路徑和名稱,eg:D:\\MyEclipse\\first\\testFile\\aa.zip  */ public static File doZip(String sourceDir, String zipFilePath)  throws IOException {    File file = new File(sourceDir);  File zipFile = new File(zipFilePath);  ZipOutputStream zos = null;  try {   // 建立寫出流操作   OutputStream os = new FileOutputStream(zipFile);   BufferedOutputStream bos = new BufferedOutputStream(os);   zos = new ZipOutputStream(bos);      String basePath = null;      // 擷取目錄   if(file.isDirectory()) {    basePath = file.getPath();   }else {    basePath = file.getParent();   }      zipFile(file, basePath, zos);  }finally {   if(zos != null) {    zos.closeEntry();    zos.close();   }  }    return zipFile; } /**   * @param source 源檔案  * @param basePath   * @param zos   */ private static void zipFile(File source, String basePath, ZipOutputStream zos)  throws IOException {  File[] files = null;  if (source.isDirectory()) {   files = source.listFiles();  } else {   files = new File[1];   files[0] = source;  }    InputStream is = null;  String pathName;  byte[] buf = new byte[1024];  int length = 0;  try{   for(File file : files) {//迴圈壓縮檔夾下面的所有檔案,知道zos.close的時候才算壓縮完成    if(file.isDirectory()) {     pathName = file.getPath().substring(basePath.length() + 1) + "/";     zos.putNextEntry(new ZipEntry(pathName));     zipFile(file, basePath, zos);    }else {     pathName = file.getPath().substring(basePath.length() + 1);     is = new FileInputStream(file);     BufferedInputStream bis = new BufferedInputStream(is);     zos.putNextEntry(new ZipEntry(pathName));     while ((length = bis.read(buf)) > 0) {      zos.write(buf, 0, length);     }    }   }  }finally {   if(is != null) {    is.close();   }  } }  /** * 解壓到指定目錄 * @param zipPath  壓縮包目錄 * @param descDir  組建檔案儲存目錄 *  */public static void unZipFiles(String zipPath,String descDir)throws IOException{unZipFiles(new File(zipPath), descDir);}/** * 解壓檔案到指定目錄 * @param zipFile * @param descDir *  */@SuppressWarnings("rawtypes")public static void unZipFiles(File zipFile2,String descDir)throws IOException{ try {         //根據ZIP檔案建立ZipFile對象         ZipFile zipFile = new ZipFile(zipFile2);            ZipEntry entry = null;            String entryName = null;            String targetFileName = null;            byte[] buffer = new byte[1024];            int bytes_read;             //擷取ZIP檔案裡所有的entry            Enumeration entrys = zipFile.entries();            //遍曆所有entry            while (entrys.hasMoreElements()) {             entry = (ZipEntry)entrys.nextElement();             //獲得entry的名字             entryName =  entry.getName();             targetFileName = descDir+File.separator+entryName;             if (entry.isDirectory()){              //  如果entry是一個目錄,則建立目錄              new File(targetFileName).mkdirs();              continue;             } else {              // 如果entry是一個檔案,則建立父目錄              new File(targetFileName).getParentFile().mkdirs();             }             //否則建立檔案             File targetFile = new File(targetFileName);             System.out.println("建立檔案:" + targetFile.getAbsolutePath());             //開啟檔案輸出資料流             FileOutputStream os = new FileOutputStream(targetFile);             //從ZipFile對象中開啟entry的輸入資料流             InputStream  is = zipFile.getInputStream(entry);             while ((bytes_read = is.read(buffer)) != -1){              os.write(buffer, 0, bytes_read);             }             //關閉流             os.close( );             is.close( );            }            System.out.println("解壓縮檔案成功!");        } catch (IOException err) {            System.err.println("解壓縮檔案失敗: " + err);        }}}


本文出自 “breezewindlw” 部落格,請務必保留此出處http://breezewindlw.blog.51cto.com/6504579/1629659

Java原始的壓縮和解壓

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.