Java Retrieves all the files in the nested compressed package (zip and rar) recursively, recursively nesting

Source: Internet
Author: User

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.

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.