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.innosystec.unrar.rarfile.FileHeader;
public class Decompressutil {
/**
* Extract zip-format compression Package
* The corresponding is 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 has a default compression encoding of UTF-8 encoding,
While WinRAR software compression is used by Windows default GBK or GB2312 encoding
Therefore, the encoding format should be developed when extracting
*/
E.setencoding ("GBK");
E.execute ();
}catch (Exception e) {
Throw e;
}
}
/**
* Unzip the RAR format compression package.
* The corresponding is Java-unrar-0.3.jar, but 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 = A.nextfileheader ();
while (Fh!=null) {
if (!fh.isdirectory ()) {
1 according to different operating systems to get the corresponding destDirName and destFileName
String compressfilename = fh.getfilenamestring (). Trim ();
String destfilename = "";
String destdirname = "";
Non-Windows systems
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 Creating a Folder
File dir = new file (destdirname);
if (!dir.exists () | |! Dir.isdirectory ()) {
Dir.mkdirs ();
}
3 Extracting files
FOS = new FileOutputStream (new File (destFileName));
A.extractfile (FH, FOS);
Fos.close ();
FOS = null;
}
FH = A.nextfileheader ();
}
A.close ();
A = null;
}catch (Exception e) {
Throw e;
}finally{
if (fos!=null) {
Try{fos.close (); fos=null;} catch (Exception e) {e.printstacktrace ();}
}
if (a!=null) {
Try{a.close (); a=null;} catch (Exception e) {e.printstacktrace ();}
}
}
}
/**
* Unzip
*/
public static void Decompress (String sourcefile,string DestDir) throws exception{
Ensure that the folder path is finally "/" or "\"
Char Lastchar = Destdir.charat (Destdir.length ()-1);
if (lastchar!= '/' &&lastchar!= ' \ \ ') {
DestDir + = File.separator;
}
According to the type, do the corresponding decompression
String type = sourcefile.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 ("Zip and rar only compression packs are supported! ");
}
}
}
Tutorial on using the tool class for Java decompression zip and rar