【Java6学习笔记】操作zip文件-阿里云开发者社区

开发者社区> 云计算> 正文

【Java6学习笔记】操作zip文件

简介:

1.ZIP文件的创建

import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
public class ZipCreate { 
    public static void main(String arg[]) { 
        ZipCreate zc = new ZipCreate(); 
        zc.create("testfile.zip",arg); 
    } 
    public void create(String zipfilename,String filename[]) { 
        byte block[] = new byte[256]; 
        int length; 
        try { 
            //FileOutputStream -> ZipOutputStream 
            FileOutputStream fos = new FileOutputStream(zipfilename); 
            ZipOutputStream out = new ZipOutputStream(fos); 
            for(int i=0; i<filename.length; i++) { 
            //FileInputStream->ZipEntry 
                FileInputStream in = new FileInputStream(filename[i]); 
                String name = filename[i].replace(File.separatorChar,'/'); 
                ZipEntry zipentry = new ZipEntry(name); 
                out.putNextEntry(zipentry);

            //in->out 
                while((length = in.read(block)) > 0) 
                    out.write(block,0,length); 
                out.closeEntry(); 
                in.close(); 
            } 
            out.close(); 
        } catch(IOException e) { 
            System.out.println(e); 
        } 
    } 
}

2.ZIP文件的读取

iimport java.util.zip.ZipFile; 
import java.util.zip.ZipEntry; 
import java.util.Enumeration; 
import java.io.IOException; 
public class ZipList { 
    public static void main(String arg[]) { 
        ZipList zl = new ZipList(); 
        zl.list("testfile.zip"); 
    } 
    public void list(String filename) { 
        try { 
            ZipFile zipfile = new ZipFile(filename); 
            Enumeration entrylist = zipfile.entries(); 
            while(entrylist.hasMoreElements()) { 
                ZipEntry zipentry = (ZipEntry)entrylist.nextElement(); 
                System.out.println(zipentry.getName()); 
            } 
            zipfile.close(); 
        } catch(IOException e) { 
            System.out.println(e); 
        } 
    } 
}


3.ZIP文件的解压

import java.util.zip.ZipFile; 
import java.util.zip.ZipEntry; 
import java.util.Enumeration; 
import java.io.InputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
public class ZipExtract { 
    public static void main(String arg[]) { 
        ZipExtract ze = new ZipExtract(); 
        ze.extract("testfile.zip"); 
    } 
public void extract(String zipfilename) { 
        byte block[] = new byte[256]; 
        int length; 
        try { 
            ZipFile zipfile = new ZipFile(zipfilename); 
            Enumeration entrylist = zipfile.entries(); 
            while(entrylist.hasMoreElements()) { 
                ZipEntry zipentry = (ZipEntry)entrylist.nextElement(); 
                InputStream in = zipfile.getInputStream(zipentry); 
String newfilename = zipentry.getName(); 
                FileOutputStream out = new FileOutputStream(newfilename); 
                while((length = in.read(block)) > 0) 
                    out.write(block,0,length); 
                in.close(); 
                out.close(); 
            } 
        } catch(IOException e) { 
            System.out.println(e); 
        } 
    } 
}




本文转自gnuhpc博客园博客,原文链接http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822258.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章