zipcompress.java

来自「输入输出程序演示例子」· Java 代码 · 共 56 行

JAVA
56
字号
/*
* File Name: ZipCompress.java
* Author:wangch
* Created Date:2004/10/09
* Function Description:Demonstrate the usage of a Zip File
*/
package com.io.zip;
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class  ZipCompress
{
	public static void main(String[] args) throws IOException
	{
    //compress the Zip File
    
		FileOutputStream   of =
      new FileOutputStream("test1.zip");
    CheckedOutputStream cus =
      new CheckedOutputStream(of,new Adler32());
    ZipOutputStream out = 
      new ZipOutputStream(
          new BufferedOutputStream(cus));
    out.setComment("A test of Java Zipping");
    BufferedReader in = 
      new BufferedReader(
        new FileReader(args[0]));
    out.putNextEntry(new ZipEntry(args[0]));//each zip file must at least have  one ZipEntry;
    int c;
    while((c=in.read())!=-1)
      out.write(c);
    in.close();
    out.close();
   
    //Extract the Zip File
    FileInputStream fi =
      new FileInputStream("test1.zip");
    CheckedInputStream csum=
      new CheckedInputStream(fi,new Adler32());
    ZipInputStream zi = 
      new ZipInputStream (
        new BufferedInputStream(csum));
    ZipEntry ze;//You must use ZipEntry to read a zip file
    while((ze=zi.getNextEntry())!=null){
      //System.out.println("Reading file"+ze);
    int x;
    while((x=zi.read()) !=-1)
       System.out.println((char)x);

    
    }
    
    zi.close();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?