六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 61|回复: 0

java解析zip

[复制链接]

升级  68%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
34
 楼主| 发表于 2013-1-15 03:11:52 | 显示全部楼层 |阅读模式
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.ArrayList;
      import java.util.Enumeration;
      import java.util.List;
      import org.apache.tools.zip.*;

public void releaseZipToFile(String sourceZip, String outFileName)
   throws IOException {
  ZipFile zfile = new ZipFile(sourceZip);
  Enumeration zList = zfile.getEntries();
  ZipEntry ze = null;
  byte[] buf = new byte[1024];
  while (zList.hasMoreElements()) {
   // 从ZipFile中得到一个ZipEntry
   ze = (ZipEntry) zList.nextElement();
   if (ze.isDirectory()) {
    continue;
   }
   // 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
   OutputStream os = new BufferedOutputStream(new FileOutputStream(
     getRealFileName(outFileName, ze.getName())));
   InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
   int readLen = 0;
   while ((readLen = is.read(buf, 0, 1024)) != -1) {
    os.write(buf, 0, readLen);
   }
   is.close();
   os.close();
   System.out.println("Extracted: " + ze.getName());
  }
  zfile.close();
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表