[JAVA100 case]040, compression and decompression file

Source: Internet
Author: User
Tags extract zip file zip

FileName: Myzip.java


import java.io.*;


import java.util.*;


import java.util.zip.*;


/**


* <p>title: Document compression and decompression </p>


* <p>description: Use Zipinputstream and zipoutputstream to file


* and directory for compression and decompression processing </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:myZip.java</p>


* @version 1.0


*/


public class myzip{


/**


*<br> Method Description: The implementation of file compression processing


*<br> input parameters: string[] FS compressed file array


*<br> return type:


*/


public void Zipfiles (string[] fs) {


try{


String fileName = fs[0];


FileOutputStream f =


New FileOutputStream (filename+ ". zip");


//Using output stream checking


Checkedoutputstream cs =


New Checkedoutputstream (F,new Adler32 ());


//Declaration output Zip stream


Zipoutputstream out =


New Zipoutputstream (New Bufferedoutputstream (CS));


//write a note


out.setcomment ("A test of Java zipping");


//compression of multiple files


for (int i=1;i<fs.length;i++) {


System.out.println ("Write file" +fs[i]);


BufferedReader in =


New BufferedReader (


new FileReader (Fs[i]);


Out.putnextentry (New ZipEntry (fs[i));


int C;


while ((C=in.read ())!=-1)


Out.write (c);


In.close ();


    }


//Turn off output stream


Out.close ();


System.out.println ("Checksum::" +cs.getchecksum (). GetValue ());


}catch (Exception e) {


System.err.println (e);


  }


 }


/**


*<br> Method Description: Extract zip file


*<br> input parameter: String filename extract zip file name


*<br> return type:


*/


public void Unzipfile (String fileName) {


try{


System.out.println ("Read zip file ...");


//File input stream


FileInputStream fi =


New FileInputStream (filename+ ". zip");


//Input flow check


checkedinputstream CSI = new Checkedinputstream (fi,new Adler32 ());


//input stream compression


Zipinputstream in2 =


New Zipinputstream (


New Bufferedinputstream (CSI));


zipentry ze;


System.out.println ("Checksum::" +csi.getchecksum (). GetValue ());


//Extract All Files


while ((Ze = in2.getnextentry ())!=null) {


System.out.println ("Reading file" +ze);


int x;


while ((x= in2.read ())!=-1)


//Here is the write file, write is output in byte.


System.out.write (x);


    }


In2.close ();


}catch (Exception e) {


System.err.println (e);


  }


 }


/**


*<br> Method Description: Read the zip file list


*<br> input parameter: String filename zip file name


*<br> return type: Vector file list


*/


public Vector listfile (String fileName) {


try{


string[] Arst=null;


vector vtemp = new vector ();


//zip File Object


ZipFile ZF = new ZipFile (filename+ ". zip");


Enumeration E = Zf.entries ();


while (e.hasmoreelements ()) {


zipentry ze2 = (zipentry) e.nextelement ();


System.out.println ("File:" +ze2);


vtemp.addelement (ZE2);


    }


return vtemp;


}catch (Exception e) {


System.err.println (e);


return null;


  }


 }


/**


*<br> Method Description: Main method


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] args) {


try{


String fileName = args[0];


myzip myzip = new Myzip ();


myzip.zipfiles (args);


Myzip.unzipfile (fileName);


Vector dd = myzip.listfile (fileName);


System.out.println ("File List:" +DD);


}catch (Exception e) {


E.printstacktrace ();


  }


 }


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: