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


    作者: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/

  • 相关阅读:
    this指针详解
    C++处理异常
    C++中的this指针
    c++中的string类
    c面试题总结
    c++中的引用详解
    c++中的new和delete
    函数重载
    BST(二叉排序树)的插入与删除
    ccf行车路线
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2822258.html
Copyright © 2020-2023  润新知