我已經寫了一些代碼來檢索zip文件並將其解壓縮爲directoy。該zip文件包含兩個文件夾,並根據每個文件所在的文件夾,將其解壓縮到該目錄的文件夾中。解壓縮文件真的很慢
但是,代碼需要很長的時間才能運行(大約10分鐘)。儘管這些文件夾每個包含近1000個文件,並且zip文件的總大小爲5000kb。我覺得它很慢,因爲我每次進入循環時都會創建FileOutputStream和InputStream。但是,我需要這樣做,因爲我不知道文件的輸出目錄,直到我從zip文件中讀取它爲止。 (即找出它在哪個文件夾)
有什麼建議嗎?
/**
* Retrieves and unzips a file from its URL
*/
public void retrieveFiles(String URL) {
//Retrieve file from URL
File zip = new File(getFile(URL));
zip.mkdirs();
try {
//Create .zip file from file directory
ZipFile zipFile = new ZipFile(zip);
Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
//While zip file contains elements, get the next zipped file
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
//Ignore folders and other zip files
if(!zipEntry.isDirectory() && !zipEntry.getName().endsWith(".zip")){
//Find directory and filename for new unzipped file
String directory = getURL(zipEntry.getName());
String fileName = getFileName(zipEntry.getName());
String fullDirectory = createDirectory(directory, fileName);
//Unzip file and store in directory
System.out.println("Unzipping file: " + fileName);
FileOutputStream fout = new FileOutputStream(fullDirectory);
InputStream in = zipFile.getInputStream(zipEntry);
for (int c = in.read(); c != -1; c = in.read()) {
fout.write(c);
}
zipFile.getInputStream(zipEntry).close();
in.close();
fout.close();
}
}
zipFile.close();
System.out.println("Unzipping complete!");
zip.delete();
} catch (IOException e) {
System.out.println("Unzip failed");
e.printStackTrace();
}
}
你不妨考慮一下http://codereview.stackexchange.com/ –
另見:[java.util.zip - 重新創建目錄結構](http:// stackoverflow .com/questions/1399126) – McDowell