Java解压缩Zip文件(支持中文)

      最近有个项目需要用Java做解压缩Zip文件,由于JDK自带的zip相关的类都不支持以中文命名的文件,所以在网上搜索了些资料,自己花了点时间研究了下。希望和大家分享下,有不足的地方还多多指教。

 

      废话不多说,直接看代码吧!(记住:在classpath里面一定要引入ant.jar这个jar包。)

 

package org.zapldy.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public abstract class ZipUtils {

 public static void zip(String source, String dest) throws IOException {
  OutputStream os = new FileOutputStream(dest);
  BufferedOutputStream bos = new BufferedOutputStream(os);
  ZipOutputStream zos = new ZipOutputStream(bos);    
  
  //支持中文,但有缺陷!这是硬编码!
  zos.setEncoding("GBK");  
  
  File file = new File(source);

  String basePath = null;
  if (file.isDirectory()) {
   basePath = file.getPath();
  } else {
   basePath = file.getParent();
  }

  zipFile(file, basePath, zos);

  zos.closeEntry();
  zos.close();
 }

 public static void unzip(String zipFile, String dest) throws IOException {
  ZipFile zip = new ZipFile(zipFile);
  Enumeration<ZipEntry> en = zip.getEntries();
  ZipEntry entry = null;
  byte[] buffer = new byte[1024];
  int length = -1;
  InputStream input = null;
  BufferedOutputStream bos = null;
  File file = null;

  while (en.hasMoreElements()) {
   entry = (ZipEntry) en.nextElement();
   if (entry.isDirectory()) {
    file = new File(dest, entry.getName());
    if (!file.exists()) {
     file.mkdir();
    }
    continue;
   }

   input = zip.getInputStream(entry);
   file = new File(dest, entry.getName());
   if (!file.getParentFile().exists()) {
    file.getParentFile().mkdirs();
   }
   bos = new BufferedOutputStream(new FileOutputStream(file));

   while (true) {
    length = input.read(buffer);
    if (length == -1)
     break;
    bos.write(buffer, 0, length);
   }
   bos.close();
   input.close();
  }
  zip.close();
 }

 private static void zipFile(File source, String basePath, ZipOutputStream zos) throws IOException {
  File[] files = new File[0];

  if (source.isDirectory()) {
   files = source.listFiles();
  } else {
   files = new File[1];
   files[0] = source;
  }

  String pathName;
  byte[] buf = new byte[1024];
  int length = 0;
  for (File file : files) {
   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);
    InputStream 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);
    }
    is.close();
   }
  }
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值