Detailed description of java zip and rar tool use tutorials, zip tool class

Source: Internet
Author: User
Tags decompress zip

Detailed description of java zip and rar tool use tutorials, zip tool class

Detailed description of java decompress zip and rar tool class use tutorials directly on the code

Package decompress; import java. io. file; import java. io. fileOutputStream; import org. apache. tools. ant. project; import org. apache. tools. ant. taskdefs. expand; import de. innosystec. unrar. archive; import de.innow.ec.unrar.rar file. fileHeader; public class DeCompressUtil {/*** unzip the zip compressed package * Corresponds to ant. jar */private static void unzip (String sourceZip, String destDir) throws Exception {try {Project p = new Project (); Expand e = new Expand (); e. setProject (p); e. setSrc (new File (sourceZip); e. setOverwrite (false); e. setDest (new File (destDir);/* the zip tool under ant is compressed by default into UTF-8 encoding, winRAR software uses windows's default GBK or GB2312 encoding for compression. Therefore, the encoding format should be formulated during decompression */e. setEncoding ("gbk"); e.exe cute () ;}catch (Exception e) {throw e ;}/ *** decompress the rar compressed package. * Corresponds to the java-unrar-0.3.jar, but the java-unrar-0.3.jar will use the commons-logging-1.1.1.jar */private static void unrar (String sourceRar, String destDir) throws Exception {Archive a = null; FileOutputStream fos = null; try {a = new Archive (new File (sourceRar); FileHeader fh =. nextFileHeader (); while (fh! = Null) {if (! Fh. isDirectory () {// 1 obtain the corresponding destDirName and destFileName String compressFileName = fh based on different operating systems. getFileNameString (). trim (); String destFileName = ""; String destDirName = ""; // non-windows if (File. separator. equals ("/") {destFileName = destDir + compressFileName. replaceAll ("\\\\", "/"); destDirName = destFileName. substring (0, destFileName. lastIndexOf ("/"); // windows system} else {destFileName = destDir + CompressFileName. replaceAll ("/", "\\\\"); destDirName = destFileName. substring (0, destFileName. lastIndexOf ("\");} // 2 create a folder File dir = new File (destDirName); if (! Dir. exists () |! Dir. isDirectory () {dir. mkdirs ();} // 3 extract the File fos = new FileOutputStream (new File (destFileName);. extractFile (fh, fos); fos. close (); fos = null;} fh =. nextFileHeader ();}. close (); a = null;} catch (Exception e) {throw e;} finally {if (fos! = Null) {try {fos. close (); fos = null;} catch (Exception e) {e. printStackTrace () ;}} if (! = Null) {try {. close (); a = null;} catch (Exception e) {e. printStackTrace () ;}}}/*** deCompress */public static void deCompress (String sourceFile, String destDir) throws Exception {// ensure that the folder path is "/" or "\" char lastChar = destDir. charAt (destDir. length ()-1); if (lastChar! = '/' & LastChar! = '\') {DestDir + = File. separator;} // extract String type = sourceFile Based on the type. substring (sourceFile. lastIndexOf (". ") + 1); if (type. equals ("zip") {DeCompressUtil. unzip (sourceFile, destDir);} else if (type. equals ("rar") {DeCompressUtil. unrar (sourceFile, destDir);} else {throw new Exception ("only zip and rar compressed packages are supported! ");}}}

  

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.