zoukankan      html  css  js  c++  java
  • 压缩

    Java提供用于压缩和解压字节流的类,这些类包含在java.util.zip 包里面,这些类也作为 Jar 文件的服务基础 ( Jar 文件是带有附加文件列表的 Zip 文件)。

    下面的程序接收一个输入文件并将之写入一个只有一项的压缩的 Zip 文件:

     import java.io.*;

    import java.util.zip.*;

    public class compress {

       public static void doit(String filein, String fileout) {

            FileInputStream fis = null;

            FileOutputStream fos = null;

            try {

                fis = new FileInputStream(filein);

                fos = new FileOutputStream(fileout);

                ZipOutputStream zos = new ZipOutputStream(fos);

                ZipEntry ze = new ZipEntry(filein);

                zos.putNextEntry(ze);

                final int BUFSIZ = 4096;

                byte inbuf[] = new byte[BUFSIZ];

                int n;

                while ((n = fis.read(inbuf)) != -1)

                     zos.write(inbuf, 0, n);

                fis.close();

                fis = null;

                zos.close();

                fos = null;

            } catch (IOException e) {

                System.err.println(e);

            } finally {

                try {

                     if (fis != null)

                         fis.close();

                     if (fos != null)

                         fos.close();

                } catch (IOException e) {

                }

            }

       }

       public static void main(String args[]) {

            if (args.length != 2) {

                System.err.println("missing filenames");

                System.exit(1);

            }

            if (args[0].equals(args[1])) {

                System.err.println("filenames are identical");

                System.exit(1);

            }

            doit(args[0], args[1]);

       }

    }

    下一个程序执行相反的过程,将一个假设只有一项的Zip文件作为输入然后将之解压到输出文件:

     import java.io.*;

    import java.util.zip.*;

    public class uncompress {

       public static void doit(String filein, String fileout) {

            FileInputStream fis = null;

            FileOutputStream fos = null;

            try {

                fis = new FileInputStream(filein);

                fos = new FileOutputStream(fileout);

                ZipInputStream zis = new ZipInputStream(fis);

                ZipEntry ze = zis.getNextEntry();

                final int BUFSIZ = 4096;

                byte inbuf[] = new byte[BUFSIZ];

                int n;

                while ((n = zis.read(inbuf, 0, BUFSIZ)) != -1)

                     fos.write(inbuf, 0, n);

                zis.close();

                fis = null;

                fos.close();

                fos = null;

            } catch (IOException e) {

                System.err.println(e);

            } finally {

                try {

                     if (fis != null)

                         fis.close();

                     if (fos != null)

                         fos.close();

                } catch (IOException e) {

                }

            }

       }

       public static void main(String args[]) {

            if (args.length != 2) {

                System.err.println("missing filenames");

                System.exit(1);

            }

            if (args[0].equals(args[1])) {

                System.err.println("filenames are identical");

                System.exit(1);

            }

            doit(args[0], args[1]);

       }

    }

    压缩是提高还是损害I/O性能很大程度依赖你的硬件配置,特别是和处理器和磁盘驱动器的速度相关。使用Zip技术的压缩通常意味着在数据大小上减少50%,但是代价是压缩和解压的时间。一个巨大(5到10 MB)的压缩文本文件,使用带有IDE硬盘驱动器的300-MHz Pentium PC从硬盘上读取可以比不压缩少用大约1/3的时间。

    压缩的一个有用的范例是向非常慢的媒介例如软盘写数据。使用高速处理器(300 MHz Pentium)和低速软驱(PC上的普通软驱)的一个测试显示压缩一个巨大的文本文件然后在写入软盘比直接写入软盘快大约50% 。

  • 相关阅读:
    为collection view添加一个补充视图(页眉或页脚)
    清除一个View控件上所有的约束
    c++学习笔记---03---从一个小程序说起2
    c++学习笔记---02---从一个小程序说起
    c++学习笔记---01---C++语言与OO思想介绍
    计算机网络(自顶向下)----读书笔记
    Android 开发笔记___基本适配器的使用__BaseAdapter
    Android 开发笔记___时间选择器---timePicker
    Android 开发笔记___DatePicker__日期选择器
    Android 开发笔记___实战项目:购物车
  • 原文地址:https://www.cnblogs.com/borter/p/9434265.html
Copyright © 2011-2022 走看看