Java Compressed Files

Source: Internet
Author: User
Tags cos crc32

Package com;

Import Java.io.BufferedInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.zip.CRC32;
Import Java.util.zip.CheckedOutputStream;

Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipOutputStream;

public class Zipcompressor {
static final int BUFFER = 8192;

Private File ZipFile;

Public Zipcompressor (String pathName) {
ZipFile = new File (pathName);
}

 public void Compress (String srcpathname) {
  file file = new file (srcpathname);
  if (!file.exists ())
   throw new RuntimeException (Srcpathname + "does not exist!) ");
  try {
   fileoutputstream fileoutputstream = new FileOutputStream (zipFile);
   checkedoutputstream cos = new Checkedoutputstream (FileOutputStream, New CRC32 ());
   zipoutputstream out = new Zipoutputstream (COS);
   string basedir = "";
   compress (file, out, basedir);
   out.close ();
  } catch (Exception e) {
   throw new runtimeexception (e);
  }
 }

private void compress (file file, zipoutputstream out, String basedir) {
/* Determine if it is a directory or a file */
if (File.isdirectory ()) {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressdirectory (file, out, basedir);
} else {
System.out.println ("Compression:" + Basedir + file.getname ());
This.compressfile (file, out, basedir);
}
}

/** Compress a directory */
private void Compressdirectory (File dir, zipoutputstream out, String basedir) {
if (!dir.exists ())
Return

file[] files = dir.listfiles ();
for (int i = 0; i < files.length; i++) {
/* Recursive */
Compress (Files[i], out, Basedir + dir.getname () + "/");
}
}

/** Compress a file */
private void Compressfile (file file, zipoutputstream out, String basedir) {
if (!file.exists ()) {
Return
}
try {
Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (file));
ZipEntry entry = new ZipEntry (Basedir + file.getname ());
Out.putnextentry (entry);
int count;
byte data[] = new Byte[buffer];
while ((count = bis.read (data, 0, BUFFER))! =-1) {
Out.write (data, 0, count);
}
Bis.close ();
} catch (Exception e) {
throw new RuntimeException (e);
}
}
}

//-------------------------------------------------------------------------------------------------------------

Here is the test

Package com;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;

Import Org.apache.tools.zip.ZipEntry;
Import Org.apache.tools.zip.ZipFile;

public class Test {

public static void Main (string[] args) {
1-----------------------------------------------------Creating a compressed file
Compress the F:/code directory into a f:/test.zip file
Zipcompressor ZC = new Zipcompressor ("f:/Chinese. zip");
Zc.compress ("F:/code");

2-----------------------------------------------------Unzip the file
FileOutputStream fileout;
File file;
InputStream InputStream;
try {
Files to unzip f:/Chinese. zip
ZipFile zipfile = new ZipFile ("f:/Chinese. zip");

   //extracted to the specified directory f:/ Self-establishing the relevant code directory
   string Path = "f:/";
   for (Enumeration entries = Zipfile.getentries (); entries.hasmoreelements ();) {
    zipentry entry = (zipentry) entries.nextelement ();
    file = new File (path + entry.getname ());
    if (Entry.isdirectory ()) {
     file.mkdirs ();
    } else {
     //created if the directory for the specified file does not exist.
     file parent = File.getparentfile ();
     if (!parent.exists ()) {
      parent.mkdirs () ;
     }

InputStream = Zipfile.getinputstream (entry);
int readedbytes = 0;
byte[] buf = new byte[1024];
Fileout = new FileOutputStream (file);
while ((Readedbytes = Inputstream.read (buf)) > 0) {
Fileout.write (buf, 0, readedbytes);
}
Fileout.close ();

Inputstream.close ();
}
}
Zipfile.close ();
} catch (IOException IoE) {
Ioe.printstacktrace ();
}
}
}

Java Compressed Files

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.