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)