Java gets recursive acquisition of all files in a nested compressed package (zip and RAR)

Source: Internet
Author: User
Tags rar

Zhangchangchang

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.

public static list<file> Getfilesofzipandrar (String zippath) throws IOException
{
String destpath = zippath.substring (0,zippath.lastindexof (".")) + "/";
Unzip the compressed file first
if (Zippath.contains (". zip"))
Unzipfile (New File (Zippath), destpath);
if (Zippath.contains (". rar"))
Unrarfile (New File (Zippath), destpath);
Go to unzip directory and unzip all zip of this directory
Recursivecompressedfile (New File (DestPath));
Get all the files in that 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
{
Breadth-First traversal
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: This problem is solved by learning: (1) Breadth first and depth first strategy, (2) method of recursive invocation, (3) How to extract Files

Reprint please indicate the source.

Java gets recursive acquisition of all files in a nested compressed package (zip and RAR)

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.