File Operations. Simple compression and decompression

Source: Internet
Author: User
Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
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. util. ArrayList;
Import java. util. Enumeration;
Import java. util. List;
Import java.util.zip. ZipEntry;
Import java.util.zip. ZipFile;
Import java.util.zip. ZipOutputStream;

Public class ziptofile {

Public static final string zip_filename = "C :\\ xjpda.zip"; // name of the file to be decompressed
Public static final string zip_dir = "C :\\ xjpda \"; // folder to be compressed
Public static final string un_zip_dir = "C :\\"; // directory of the file to be decompressed
Public static final int buffer = 1024; // cache size

/**
* Zip compression function.
* Compress all files in the basedir (Folder directory), including subdirectories.
* @ Throws exception
*/
Public static void zipfile (string basedir, string filename) throws exception {
List filelist = getsubfiles (new file (basedir ));
ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (fileName ));
ZipEntry ze = null;
Byte [] buf = new byte [BUFFER];
Int readLen = 0;
For (int I = 0; I <fileList. size (); I ++ ){
File f = (File) fileList. get (I );
Ze = new ZipEntry (getAbsFileName (baseDir, f ));
Ze. setSize (f. length ());
Ze. setTime (f. lastModified ());
Zos. putNextEntry (ze );
InputStream is = new BufferedInputStream (new FileInputStream (f ));
While (readLen = is. read (buf, 0, BUFFER ))! =-1 ){
Zos. write (buf, 0, readLen );
}
Is. close ();
}
Zos. close ();
}

/**
* If the root directory is specified, the relative path of another file name is returned for the zip file.
* @ Param basedir java. Lang. String root directory
* @ Param realfilename actual file name of Java. Io. File
* @ Return relative file name
*/
Private Static string getabsfilename (string basedir, file realfilename ){
File real = realfilename;
File base = new file (basedir );
String ret = real. getname ();
While (true ){
Real = real. getparentfile ();
If (real = NULL)
Break;
If (Real. Equals (base ))
Break;
Else
Ret = real. getname () + "/" + ret;
}
Return ret;
}

/**
* Obtain the list of all objects in the specified directory, including subdirectories.
* @ Param basedir file specifies the Directory
* @ Return List containing java. io. File
*/
Private static List getSubFiles (File baseDir ){
List ret = new ArrayList ();
File [] tmp = baseDir. listFiles ();
For (int I = 0; I <tmp. length; I ++ ){
If (tmp [I]. isFile ())
Ret. add (tmp [I]);
If (tmp [I]. isDirectory ())
Ret. addAll (getSubFiles (tmp [I]);
}
Return ret;
}

/**
* Decompression function.
* Decompress the ZIP_FILENAME file to the ZIP_DIR directory.
* @ Throws Exception
*/
Public static void upZipFile () throws Exception {
ZipFile zfile = new ZipFile (ZIP_FILENAME );
Enumeration zList = zfile. entries ();
ZipEntry ze = null;
Byte [] buf = new byte [1, 1024];
While (zList. hasMoreElements ()){
Ze = (ZipEntry) zList. nextElement ();
If (ze. isDirectory ()){
File f = new File (ZIP_DIR + ze. getName ());
F. mkdir ();
Continue;
}
OutputStream OS = new BufferedOutputStream (new FileOutputStream (getRealFileName (ZIP_DIR, ze. getName ())));
InputStream is = new BufferedInputStream (zfile. getInputStream (ze ));
Int readLen = 0;
While (readLen = is. read (buf, 0, 1024 ))! =-1 ){
OS. write (buf, 0, readLen );
}
Is. close ();
OS. close ();
}
Zfile. close ();
}

/**
* If the root directory is specified, the actual file name corresponding to a relative path is returned.
* @ Param baseDir specify the root directory
* @ Param absFileName relative path name, from name in ZipEntry
* @ Return java. io. File actual File
*/
Public static File getRealFileName (String baseDir, String absFileName ){
String [] dirs = absFileName. split ("/");
File ret = new File (baseDir );
If (dirs. length> 1 ){
For (int I = 0; I <dirs. length-1; I ++ ){
Ret = new File (ret, dirs [I]);
}
If (! Ret. exists ())
Ret. mkdirs ();
Ret = new File (ret, dirs [dirs. length-1]);
Return ret;
}
Return ret;
}

}

However, there are Chinese problems above.
This is because the format conversion in java.util.zip is incorrect.
Set
Import java.util.zip. ZipEntry;
Import java.util.zip. ZipFile;
Import java.util.zip. ZipOutputStream;
Change
Import org.apache.tools.zip .*;
You can!
Apache corrected this phenomenon.

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.