Atitit.java extracting A list of zip rar files
1. Take a zip rar file Scenario issue 1
1.1. Problems with multiple compression 1
1.2. Problem with file name encoding 1
1.3. Determination of the catalogue 1
2. rar parsing 1
2.1. JAVA uses Java-unrar-0.3.jar to extract rarandsolve Chinese garbled (recommended) 2
2.2. Mucommande 2
3.-------Code 2
3.1. Rrar Code Rarx 4
4. Reference 5
1. Take the zip rar file Scene problem 1.1. Problems with multiple compression
In order to obtain a compressed package of files, and the package may contain compressed packets, folders, folders containing compressed packets, files and other nested cases, the use of breadth-first traversal and depth-first traversal method to solve the problem.
Gets the list of files in the archive package for the multiple-compression package: If it is a level two sub-compressed package, you need to extract to the temp directory, and then single-layer get: You cannot get the child compression package directory directly.
1.2. Problem with file name encoding
There is a coding problem with the JDK. Using Apache 's package, you can use GBK encoding.
1.3. Determination of the catalogue
directory, which is also in the same sibling as the child file. Directly next.
author :: Old Wow's claws Attilax Ayron, Email:[email protected]
reprint Please indicate source: Http://blog.csdn.net/attilax
2. RAR parsing
How do I extract or unzip a rar archive in Java ? Previously looked up a lot of information, did not find the relevant third-party library, the online search for data said to parse rar can only be more rar command line parameters to parse. Because the internal structure of the RAR Archive document is not co-opened. Therefore, there is no dedicated parser library program. I have to use the command line, but in the actual application, encountered RAR Encryption encountered a problem.
So oneself again on the internet very bother to find the relevant information, finally found a library can parse rar document.
Library:http://www.mucommander.com/. This is a software that solves multiple documents and is written in Java . So it can be quoted to solve the RAR parsing.
Mucommander 9 Three of the five or six API out there. 8mucommander-v0.82.rar three. There are two less package. Tender to admire a few pkg . Bad download on the official website. Direct download, but a separate software, to the good quality of Tibet ... giveup
2.1. JAVAUseJava-unrar-0.3.jarUnziprar, and solve the Chinese garbledRecommended
There is a project need to extract rar, Internet search a bit of people are using Java-unrar-0.3.jar , so wrote a util method extracting rar
2.2.Mucommande
3.-------Code
PRJ. atibrow. zipx
/**
* @param args
*/
Public Static void Main (string[] args) {
Try {
Final String zipfilename = "E:\\00\\product3.zip";
Readzipfile (zipfilename, "GBK", new Closure2 () {
@Override
Public Object execute (Object arg0) {
System. out. println ("unziping" +zipfilename+"/"+arg0);
return null;
}
});
} Catch (Exception e) {
// TODO auto-generated Catch block
e. Printstacktrace ();
}
}
Unziping E:\00\product3.zip/product3/product.sql
Unziping E:\00\product3.zip/product3/proxy2.zip
Unziping e:\00\product3.zip/product3/dir2/Export-all terms ATI pc in Com.rar
Public Static void readzipfile (string zipfilename, string encode,
Closure2 Clos) {
// = " UTF -8 ";
String Path = new File (zipfilename). GetParent ();
Org.apache.tools.zip.ZipFile ZipFile = null;
Try {
ZipFile = new zipfile (zipfilename, encode); //, encode
} Catch (IOException E1) {
// TODO auto-generated Catch block
E1. Printstacktrace ();
}
New Org.apache.tools.zip.ZipFile (Zipfilename);
Java.util.Enumeration E = zipfile. getentries ();
Org.apache.tools.zip.ZipEntry zipentry = null;
while (e. hasmoreelements ()) {
zipentry = (zipentry) E.Nextelement ();
String zipFileName2 = zipentry. GetName ();
// path_now=zipfilename2;
if (zipentry. isdirectory ())
{
continue;
}
Clos. Execute (zipFileName2);
}
}
3.1.Rrar CodeRarx
PRJ. atibrow. Rarx
*/
Public class Rarx {
Public Static void Main (string[] args) throws Exception {
Unrar ("E:\\down\\51vpn.rar", new Closure2 () {
@Override
Public Object execute (Object arg0) {
System. out. println (arg0);
return null;
}
});
}
/**
*} unzip the rar format compression package.
* corresponds to Java - Unrar - 0.3.jar, but Java - Unrar - 0.3.jar will also use the Commons - Logging - 1.1.1.jar
*/
Private Static void Unrar (String sourcerar, Closure2 cls) {
Archive a = null;
FileOutputStream fos = null;
Try {
a = new Archive (new File (sourcerar));
} Catch (rarexception e) {
// TODO auto-generated Catch block
e. Printstacktrace ();
Throw New RuntimeException (e);
} Catch (IOException e) {
// TODO auto-generated Catch block
e. Printstacktrace ();
Throw New RuntimeException (e);
}
Fileheader fh = a. Nextfileheader ();
while (fh ! = null) {
if (! FH. Isdirectory ()) {
CLS. Execute (FH. getfilenamestring ());
}
fh = a. Nextfileheader ();
}
Try {
a. Close ();
} Catch (IOException e) {
// TODO auto-generated Catch block
e. Printstacktrace ();
}
a = null;
}
}
4. Reference
How does Java read the files inside Zip and RAR ? _ Baidu knows . htm
Java gets recursion to get all the files in the nested compressed package (Zip and rar) -java- Seventh City . HTM
Java extracting text data from RAR documents -Wqjsir 's Column - blog channel -CSDN.NET.htm
Atitit. compression Algorithm ZLib, gzip, zip Best Practices java. NET Php-attilax 's Column - Blog Channel -CSDN.NET.htm
Atitit. Unzip Zip implementation best Practices for files Java C #. NET Php-attilax 's Column - Blog Channel -CSDN.NET.htm
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Atitit. Extracting a list of zip RAR files Principles and design of Java PHP C #