Java compression and decompression zip, Jar, gzip file Instance code _java

Source: Internet
Author: User

We often use compression software such as WinZip to compress files for easy transmission. In Java also provides a file compression to reduce the amount of data transmission of the class, you can easily compress the file into a zip, JAR, gzip, and other forms, gzip mainly in the Linux system under the compressed file.

The following is mainly about zip-style compressed files, and jars, gzip-type compressed files are similar usage.

Zip is a very common form of compression, in Java to implement ZIP compression is the main use of java.util.zip this package inside the class. Mainly include ZipFile, Zipoutputstream, Zipinputstream and ZipEntry. Zipoutputstream is used to compress files, Zipinputstream and ZipFile are used to decompress files, and in the process of compressing and decompressing, zipentry are used. In a Java zip-compressed file, each child file is a ZipEntry object.

Compressed files:

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.nio.charset.Charset;
Import Java.util.zip.ZipEntry;

Import Java.util.zip.ZipOutputStream;
 public class Zipoutputstreamtest {public static void main (String args[]) throws IOException {test1 ();
 Test2 (); The public static void Test1 () throws IOException {Zipoutputstream Zos = new Zipoutputstream (New FileOutputStream ("d:\\
 Testzip.zip "), Charset.forname (" GBK "));
 Instantiate a ZipEntry object named Ab.txt zipentry entry = new ZipEntry ("Ab.txt");
 Set up comment zos.setcomment ("Zip test for Single file");
 Add the generated ZipEntry object to the compressed file, and the content written to the compressed file will be placed inside the ZipEntry object Zos.putnextentry (entry);
 InputStream is = new FileInputStream ("D:\\ab.txt");
 int len = 0;
 while (len = Is.read ())!=-1) zos.write (len);
 Is.close ();
 Zos.close ();
 The public static void Test2 () throws IOException {file InFile = new file ("D:\\test"); Zipoutputstream Zos = new ZipoutputstreaM (New FileOutputStream ("D:\\test.zip"), Charset.forname ("GBK"));
 Zos.setcomment ("Multi-document Processing");
 ZipFile (InFile, Zos, "");
 Zos.close (); public static void ZipFile (File inFile, Zipoutputstream Zos, String dir) throws IOException {if (infile.isdirectory
  ()) {file[] files = infile.listfiles ();
 for (file file:files) zipfile (file, Zos, dir + "\" + infile.getname ());
  else {String entryname = null; if (! "".
  Equals (dir)) EntryName = dir + "\" + infile.getname ();
  else EntryName = Infile.getname ();
  ZipEntry entry = new ZipEntry (entryname);
  Zos.putnextentry (entry);
  InputStream is = new FileInputStream (inFile);
  int len = 0;
  while (len = Is.read ())!=-1) zos.write (len);
 Is.close ();

 }

 }
 
}

uncompress files:

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.nio.charset.Charset;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;

Import Java.util.zip.ZipInputStream; public class Zipinputstreamtest {public static void main (String args[]) throws IOException {File File = new file ("D:\ \test.zip ");//Compressed file ZipFile ZipFile = new ZipFile (file);//Instantiate zipfile, each zip compressed file can be represented as a zipfile// Instantiate a Zipinputstream object of a Zip compressed file, which can take advantage of the Getnextentry () method of the class to get each ZipEntry object in turn zipinputstream Zipinputstream = new
 Zipinputstream (new FileInputStream (file), Charset.forname ("GBK"));
 ZipEntry zipentry = null;
  while ((ZipEntry = Zipinputstream.getnextentry ())!= null) {String fileName = Zipentry.getname ();
  File temp = new file ("d:\\unpacktest\\" + fileName);
  if (! Temp.getparentfile (). exists ()) Temp.getparentfile (). Mkdirs (); OutputStream OS = new FileOutputStream (temp);
  The input stream InputStream is = Zipfile.getinputstream (zipentry) of specific zipentry is obtained by ZipFile getInputStream method;
  int len = 0;
  while (len = Is.read ())!=-1) os.write (len);
  Os.close ();
 Is.close ();
 } zipinputstream.close ();





 }
 
}

The above is the Java compression and decompression file data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!

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.