六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 42|回复: 0

将多个文件压缩成zip(zip文件操作大全)

[复制链接]

升级  72%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
36
 楼主| 发表于 2013-1-15 01:58:58 | 显示全部楼层 |阅读模式
 String[] fileNames = {"11.txt","22.txt"};   
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("tomorrow.zip"));   
  
byte[] buff = new byte[1024];   
for(int i = 0;i < fileNames.length ; i++)   
{   
    FileInputStream is = new FileInputStream(fileNames[i]);   
       
    zos.putNextEntry(new ZipEntry(fileNames[i]));        //最关键的是这一步   
    int len = 0;   
    while ((len = is.read(buff)) > 0 )   
    {   
        zos.write(buff, 0, len);   
    }   
       
    zos.closeEntry();        //需要注意的是这个地方   
    is.close();   
}   
zos.close();  
   2.罗列某个压缩文件中所有的文件名称

 ZipFile zf  = new ZipFile(new File("tomorrow.zip"));   
for(Enumeration<ZipEntry> entrys = (Enumeration<ZipEntry>)zf.entries() ; entrys.hasMoreElements();)   
{   
    String zipEnteyName = entrys.nextElement().getName();   
    System.out.println(zipEnteyName);   
}  
 3. 解压缩ZIP文件
ZipInputStream ziStream = new ZipInputStream(new FileInputStream("tomorrow.zip"));   
//主要是为了循环使用   
ZipFile zFile = new ZipFile(new File("tomorrow.zip"));   
  
for (Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zFile.entries();entries.hasMoreElements();)   
{   
    ZipEntry entry = ziStream.getNextEntry();   
    if ( null == entry)   
    {   
        break;   
    }   
    String name = entry.getName();   
    System.out.println(name);   
    FileOutputStream os = new FileOutputStream(name);   
    int len = 0;   
    byte[] buff = new byte[1024];   
    while ((len = ziStream.read(buff)) > 0)   
    {   
        os.write(buff, 0, len);   
    }   
    ziStream.closeEntry();   
    os.close();   
}   
  
ziStream.close(); 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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