Java compressed gzip for simple compression, zip for multi-file saving

Source: Internet
Author: User
Tags cos

Java compressed gzip for simple compression, zip for multi-file saving

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbglhbmdydwkxotg4/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Package Org.rui.io.compress;import Java.io.bufferedoutputstream;import Java.io.bufferedreader;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.inputstreamreader;import Java.util.zip.gzipinputstream;import java.util.zip.gzipoutputstream;/** * Simple compression with gzip * @author Lenovo * */public class Gzipcompress {static String path= "d:\\users\\liangrui\\workspace\\thinking \\src\\org\\rui\\io\\compress\\ "; static String [] arg=new string[]{path+" Gzipcompress.java "};p ublic static void Main ( String[] args) throws Exception {//inbufferedreader in=new bufferedreader (New FileReader (arg[0]));// Outbufferedoutputstream out=new Bufferedoutputstream (New Gzipoutputstream (New FileOutputStream ("test.gz")); System.out.println ("Writing file"), int c;while ((C=in.read ())!=-1) {out.write (c);} Closein.close (); Out.close (); System.out.println ("Reading file================"); BufferedReader br=new BufferedReader (New InputStreamReader (New GzipinputstreamM ("test.gz"))); String S;while ((S=br.readline ())!=null) System.out.println (s);}}




Package Org.rui.io.compress;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.bufferedreader;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Import Java.io.fileoutputstream;import Java.io.filereader;import Java.util.enumeration;import Java.util.zip.adler32;import Java.util.zip.checkedinputstream;import Java.util.zip.checkedoutputstream;import Java.util.zip.zipentry;import Java.util.zip.zipfile;import Java.util.zip.zipinputstream;import java.util.zip.zipoutputstream;/** * Multi-File Save with Zip * @author Lenovo * */public class Zipcompress {//static String path= "d:\\u Sers\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\compress\\ "; static String path=new File (" "). GetAbsolutePath () + "\\src\\org\\rui\\io\\compress\\"; static String [] arg=new string[]{path+ "Gzipcompress.java", path+ "Zipcompress.java"};p ublic static void Main (string[] args) throws Exception {//outfileoutputstream f=new FileOutputStream ("Test.zip"); CheckedoutpuTStream cos=new Checkedoutputstream (f,new Adler32 ()); Zipoutputstream zos=new zipoutputstream (COS);//outbufferedoutputstream out=new Bufferedoutputstream (ZOS); Zos.setcomment ("A test of Java zipping"); for (String S:arg) {System.out.println ("writing file" +s); BufferedReader in =new BufferedReader (new FileReader (s));//zos.putnextentry (new ZipEntry (s)); int C;while ((C=in.read ( ))!=-1) {out.write (c);} In.close (); Out.flush ();} Out.close ();//checksum valid only after the file has been closed! System.out.println ("Reading file================"); FileInputStream fi=new FileInputStream ("Test.zip"); Checkedinputstream csumi=new Checkedinputstream (fi,new Adler32 ()); Zipinputstream zis=new Zipinputstream (Csumi); Bufferedinputstream bis=new Bufferedinputstream (ZiS); ZipEntry Ze;while ((Ze=zis.getnextentry ())!=null) {System.out.println ("Reading file" +ze); int X;while ((X=bis.read ()) !=-1) {//system.out.print ((char) x); System.out.write (x);}} if (arg.length==1) System.out.println ("Checksum:" +csumi.getchecksum (). GetValue ());Bis.close ();//alternative to open and read zip fileszipfile zf=new zipfile ("Test.zip"); Enumeration E=zf.entries (); while (E.hasmoreelements ()) {ZipEntry ze2= (zipentry) e.nextelement (); System.out.println ("File:" +ze2);//...and extract the data as Before}/*if (arg.length==1) */}}












Java compressed gzip for simple compression, zip for multi-file saving

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.