Java Retrieves all the files in the nested compressed package (zip and rar) recursively, recursively nesting
Author: Zhang changchang
To obtain files in a compressed package, the package may contain various nesting conditions, such as packages, folders, and folders, this problem is solved using the methods of breadth-first traversal and depth-first traversal.
Public static List <File> getFilesOfZipAndRar (String zipPath) throws IOException
{
String destPath = zipPath. substring (0, zipPath. lastIndexOf (".") + "/";
// Decompress the compressed file first
If (zipPath. contains (". zip "))
UnZipFile (new File (zipPath), destPath );
If (zipPath. contains (". rar "))
UnRarFile (new File (zipPath), destPath );
// Enter the decompressed directory and decompress all the zip files in the directory.
RecursiveCompressedFile (new File (destPath ));
// Obtain all the files in the directory
Iterator iterator = Directory. walk (destPath). iterator ();
List <File> files = new ArrayList <File> ();
File file = null;
While (iterator. hasNext ())
{
File = (File) iterator. next ();
If (file. getName (). contains (". rar "))
Continue;
Files. add (file );
}
Return files;
}
Public static void recursiveCompressedFile (File file) throws IOException
{
// Traverse the breadth first
For (File e: file. listFiles ())
{
String filePath = e. getAbsolutePath (). replace ("\\\\","/");
// Depth-first Traversal
If (e. getName (). contains (". zip "))
{
String desString = filePath. substring (0, filePath. lastIndexOf (".") + "/";
UnZipFile (e, desString );
E. delete ();
RecursiveCompressedFile (new File (desString ));
}
If (e. getName (). contains (". rar "))
{
String desString = filePath. substring (0, filePath. lastIndexOf (".") + "/";
UnRarFile (e, desString );
E. delete ();
RecursiveCompressedFile (new File (desString ));
}
If (e. isDirectory ())
RecursiveCompressedFile (e );
}
}
Summary: I learned the following methods to solve the problem: (1) breadth-first and depth-first strategies; (2) recursive calling methods; (3) how to extract files
Reprinted to describe the source.
Linux cool help to decompress the problem
Recursive compression is meaningless. However, you can only decompress the package one by one.
If there are not many, you can do it manually;
If there are many nested compression tasks, you can use scripts.
Java compression cannot Compress folders
Yes. Press the folder. You have a problem.
If it is not an empty folder.
ZipEntry ("/A2F0/folder/file.txt ");
In this way, press a file, so that the multi-layer file stuff is also pressed in.
ZipEntry ("/A2F0/folder /");
In this case, a folder is pressed.