我的zip文件,我有幾個zip文件,每個包含多個文件太多,我想使用ZipInputStream類提取。其中有一些圖像。當我嘗試使用BufferedOutputStream提取這些圖像時,它們會部分解壓縮,並且圖像不完整。我不能完全提取使用ZipInputStream
private void extractArchives() {
ZipInputStream zis;
File archiveDir = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/archives/");
File[] files = archiveDir.listFiles();
for (int i = 0; i < files.length; ++i)
{
File file = files[i];
try
{
zis = new ZipInputStream(new FileInputStream(file));
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null)
{
BufferedOutputStream bos;
byte[] buffer = new byte[102400];
int count;
while ((count = zis.read(buffer)) != -1)
{
String fileName = ze.getName();
if (fileName.endsWith(".jpg"))
{
path += File.separator + fileName;
bos = new BufferedOutputStream(new FileOutputStream(path));
bos.write(buffer, 0, count);
bos.close();
}
}
}
zis.close();
}
catch(FileNotFoundException e) { continue; }
//If the file is not a zip file or is a directory
catch (IOException e) { continue; }
}
}
上面的代碼有什麼問題嗎?使用BufferedOutputStream會導致這個問題嗎?我很欣賞任何想法。謝謝。
哪裏變量'files','path'和'zis'申報?你在你的來源除了你的解壓縮問題有一個明確的問題:你總是附加到您的路徑('路徑+ =文件分割符+ fileName')這樣的路徑名變得越來越長,但你沒有創建任何新的目錄。 –
是的,你說得對。我應該改正這一點。不過,我嘗試了一個zip文件和一個圖像,但圖像仍未完全提取。 –