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! ");}}}