一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

支持中文Java压缩与解压zip文件程序代码

时间:2013-09-27 编辑:简简单单 来源:一聚教程网


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();
        }
 }
 
}

原文来自:应用开发笔记

热门栏目