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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

gnuhpc 2012-12-17 20:35:00 浏览628
展开阅读全文

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

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/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

网友评论

登录后评论
0/500
评论
gnuhpc
+ 关注