支援中文Java壓縮與解壓zip檔案程式碼

來源:互聯網
上載者:User


apache ant下載地址:

http://ant.apache.org/bindownload.cgi

把lib/ant.jar放到我們項目的構建路徑中,只需要ant.jar。其實ant的zip API與jdk的高度相似,如果之前是用jdk的api寫的,基本上只要更改頂部的import包就可以了

 代碼如下 複製代碼


package common;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.List;
 
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
 
public class ZipUtils {
 public static void main(String[] args) throws Exception {
  unzipPSD("/media/share/material/file/temp/1eeb28ecb8e0d6f2.zip","/media/share/material/file/temp/");
 
 }
 
 /**
  * 解壓zip,
  *
  * @param zipFile 裡面可以有多個psd檔案,支援多級目錄
  * @param targetPath 儲存目錄
  * @throws Exception
  */
 public static void unzipPSD(String zipPath, String targetPath) throws Exception {
 
   ZipFile zipFile = new ZipFile(zipPath);
   Enumeration emu = zipFile.getEntries();
   int i = 0;
   while (emu.hasMoreElements()) {
    ZipEntry entry = (ZipEntry) emu.nextElement();
 
    String fileName=entry.getName().toLowerCase();
    if(!fileName.startsWith("__macosx/")&&fileName.endsWith("psd"))
    {
     //如果檔案名稱沒有以__macosx/開頭,且以psd結尾,就是psd檔案,解壓,在mac下壓縮的檔案,會自動加上__macosx目錄,但其實是沒用的
     BufferedInputStream bis = new BufferedInputStream(
       zipFile.getInputStream(entry));
     File file = new File(targetPath + System.currentTimeMillis()+".psd");
     //一次讀40K
     int BUFFER=40960;
     FileOutputStream fos = new FileOutputStream(file);
     BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);
 
     int count;
     byte data[] = new byte[BUFFER];
     while ((count = bis.read(data, 0, BUFFER)) != -1) {
      bos.write(data, 0, count);
     }
     bos.flush();
     bos.close();
     bis.close();
    }
   }
   zipFile.close();
 
 }
 
 /**
  * 壓縮多個檔案
  * @param zipPath
  * @param filePaths
  */
 public static void zipFiles(String zipPath,List filePaths)
 {
  try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipPath);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
         int BUFFER=40960;
            byte data[] = new byte[BUFFER];
 
            for(String filePah:filePaths)
            {
             File file=new File(filePah);
                FileInputStream fi = new FileInputStream(file);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(file.getName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
            out.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 }
 
}

原文來自:應用開發筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.