我們如何檢查是否zip文件損壞或有效的Zip文件將其解壓縮ZipInputStream檢查是否zip文件有效,纔去提取它
我code`
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public void unzip() {
FileInputStream fin = null;
ZipInputStream zin = null;
OutputStream fout = null;
File outputDir = new File(_location);
File tmp = null;
try {
fin = new FileInputStream(_zipFile);
zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.d("Decompress", "Unzipping " + ze.getName());
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
tmp = File.createTempFile("decomp", ".tmp", outputDir);
fout = new BufferedOutputStream(new FileOutputStream(tmp));
DownloadFile.copyStream(zin, fout, _buffer, BUFFER_SIZE);
zin.closeEntry();
fout.close();
fout = null;
tmp.renameTo(new File(_location + ze.getName()));
tmp = null;
}
}
zin.close();
zin = null;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (tmp != null ) { try { tmp.delete(); } catch (Exception ignore) {;} }
if (fout != null) { try { fout.close(); } catch (Exception ignore) {;} }
if (zin != null ) { try { zin.closeEntry(); } catch (Exception ignore) {;} }
if (fin != null ) { try { fin.close(); } catch (Exception ignore) {;} }
}
}
`
此之前,工作正常與有效的zipfile,但無效的zipfile它doesen't拋出任何異常不產生任何東西,但我需要在解壓縮之前確認zip文件的有效性