import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

public class Test {
    public static void main(String[] args){
//        GZIPCompress.test();
        ZIPCompress.test();
    }
}

class GZIPCompress {
    public static void test() {
        try {
            //创建一个字符输入流
            BufferedReader in = new BufferedReader(new FileReader("temp.tmp"));

            //创建一个压缩输出流
            BufferedOutputStream out = new BufferedOutputStream(
                    new GZIPOutputStream(
                            new FileOutputStream("temp1.tmp")));

            System.out.println("Writing File...");

            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }

            in.close();
            out.close();

            System.out.println("Reading File...");
            BufferedReader in2 = new BufferedReader(
                    new InputStreamReader(
                            new GZIPInputStream(
                                    new FileInputStream("temp1.tmp"))));
            String s;
            while ((s = in2.readLine()) != null) {
                System.out.println(s);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class ZIPCompress {
    public static void test() {
        String[] args = new String[]{
                "temp.tmp",
                "temp1.tmp",
                "temp2.tmp",
                "temp3.tmp",
                "temp4.tmp",
        };
        try {
            //进行压缩
            FileOutputStream f = new FileOutputStream("test.zip");
            CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
            ZipOutputStream zos = new ZipOutputStream(csum);
            BufferedOutputStream out = new BufferedOutputStream(zos);

            zos.setComment("A Test Of Java Zipping...");

            for (String arg : args) {
                System.out.println("Writing file: " + arg);
                BufferedReader in = new BufferedReader(new FileReader(arg));
                zos.putNextEntry(new ZipEntry(arg));
                int c;
                while ((c = in.read()) != -1) {
                    out.write(c);
                }
                in.close();
                out.flush();
            }
            out.close();

            System.out.println("Checksum:"+csum.getChecksum().getValue());

            //进行解压
            /*
            System.out.println("Reading File...");
            FileInputStream fi = new FileInputStream("test.zip");
            CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
            ZipInputStream in = new ZipInputStream(csumi);
            BufferedInputStream bis = new BufferedInputStream(in);

            ZipEntry ze;
            while ((ze = in.getNextEntry()) != null) {
                System.out.println("Reading File: " + ze);
                int x;
                while ((x = bis.read()) != -1) {
                    System.out.println(x);
                }
            }
            bis.close();
            */
            //查看压缩文件的目录
            ZipFile zf = new ZipFile("test.zip");
            Enumeration e = zf.entries();
            while (e.hasMoreElements()) {
                ZipEntry ze2 = (ZipEntry)e.nextElement();
                System.out.println("File: "+ze2);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最新文章

  1. APIPA(Automatic Private IP Addressing,自动专用IP寻址)
  2. ListView设置headerview和footerview
  3. java 24 - 9 GUI 之 给窗体换图标、设置启动在屏幕中间、更换皮肤
  4. 黑马程序员:Java编程_多线程
  5. Java GC系列(4):垃圾回收监视和分析
  6. 安装webmin
  7. outlook圆角table
  8. 另类方法解决设计Web页面出现:Error Creating Control
  9. npm scripts 助力前端开发,实时刷新浏览器
  10. gc内存回收机制
  11. jquery倒计时(仿团购)转至 http://justcoding.iteye.com/blog/2210962
  12. QT 报错:Project ERROR: Xcode not set up properly. You may need to confirm the license agreement by running /usr/bin/xcodebuild.
  13. “一切都是消息”--MSF(消息服务框架)之【请求-响应】模式
  14. 机器学习(2) - KNN识别MNIST
  15. 第36节:Java当中的线程
  16. github for Mac 教程
  17. ACM退役前2个月总结
  18. 警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
  19. [leetcode]632. Smallest Range最小范围
  20. 【wepy实战】wepy搭建完整项目

热门文章

  1. Android零基础入门第81节:Activity数据传递
  2. Qt在各平台上的搭建qt-everywhere(Qt for windows7-64bit, Ubuntu 12.04-32bit, 嵌入式x86平台, 嵌入式arm平台)
  3. Qt加载百度离线地图
  4. 使用C++还是QML(QML容易使用和维护,效果好)
  5. 系列教程 - java web开发
  6. 一步一步教你用IntelliJ IDEA 搭建SSM框架(3)——实现用户登录功能
  7. Jmeter 如何把数据库的数据依次获取作为参数传入下一个请求?附栗子
  8. spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)
  9. 你竟然没用 Maven 构建项目?
  10. ssm中mapper注入失败的传奇经历