android zip解压出错,Android:解压缩文件会引发数据错误或CRC错误
我正在開發一個下載zip文件并在本地解壓縮的項目.我遇到的問題是解壓縮過程在5%的時間內起作用.
在這一點上,這對我來說是一個謎,因為有時它可以工作,但大多數時候它會拋出數據或crc錯誤.即使zip文件沒有改變,它甚至會在錯誤之間切換.
我嘗試過由眾多工具創建的zip文件,想知道格式是否不正確.但無濟于事.即使在終端中創建的拉鏈也不起作用.
這是我的解壓縮代碼:
try {
String _location = model.getLocalPath();
FileInputStream fin = new FileInputStream(localFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
byte[] buffer = new byte[1024];
while((ze = zin.getNextEntry()) != null) {
if(_cancel) break;
System.out.println("unzipping " + ze.getName());
if(ze.isDirectory()) {
File f = new File(_location + ze.getName());
f.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for(int c = zin.read(buffer); c > 0; c = zin.read(buffer)) {
fout.write(buffer,0,c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
if(_cancel) {
handler.post(dispatchCancel);
return;
}
} catch(Exception e) {
System.out.println("UNZIP ERROR!");
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}
以下是我通常創建zip文件的方法.
$>zip -r myzip.zip myzip/
以下是兩個錯誤輸出:
java.util.zip.ZipException: CRC mismatch
at java.util.zip.ZipInputStream.readAndVerifyDataDescriptor(ZipInputStream.java:209)
at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:173)
at com.XX.XX.XXIssueDownloader$7.run(XXIssueDownloader.java:222)
at java.lang.Thread.run(Thread.java:1020)
java.util.zip.ZipException: data error
at java.util.zip.ZipInputStream.read(ZipInputStream.java:336)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at com.XX.XX.XXIssueDownloader$7.run(XXIssueDownloader.java:219)
at java.lang.Thread.run(Thread.java:1020)
任何人都知道為什么我會得到這些錯誤?我沒有隨處可見.
解決方法:
加載Zip文件時有兩件事非常重要.
>確保您使用的是不包含Accept-Encoding:標頭的請求方法.如果它在請求中,那么響應不是zip文件,它是一個gzip壓縮的zip文件.因此,如果您在下載時將其直接寫入磁盤,那么它實際上不會是一個zip文件.你可以使用這樣的東西來加載zip文件:
URL url = new URL(remoteFilePath);
URLConnection connection = url.openConnection();
InputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream f = new FileOutputStream(localFile);
//setup buffers and loop through data
byte[] buffer = new byte[1024];
long total = 0;
long fileLength = connection.getContentLength();
int len1 = 0;
while((len1 = in.read(buffer)) != -1) {
if(_cancel) break;
total += len1;
_Progress = (int) (total * 100 / fileLength);
f.write(buffer,0,len1);
handler.post(updateProgress);
}
f.close();
in.close();
>使用輸入和輸出流時,不要使用讀(緩沖)或寫(緩沖)方法,需要使用讀/寫(緩沖區,0,len).否則,您正在編寫或閱讀的內容最終可能會包含垃圾數據.前者(讀取(緩沖區))將始終讀取整個緩沖區,但實際上可能沒有完整的緩沖區,例如,如果循環的最后一次迭代只讀取512字節.所以這是你解壓縮文件的方式:
String _location = model.getLocalPath();
FileInputStream fin = new FileInputStream(localFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while((ze = zin.getNextEntry()) != null) {
if(_cancel) break;
System.out.println("unzipping " + ze.getName());
System.out.println("to: " + _location + ze.getName());
if(ze.isDirectory()) {
File f = new File(_location + ze.getName());
f.mkdirs();
} else {
byte[] buffer2 = new byte[1024];
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for(int c = zin.read(buffer2); c > 0; c = zin.read(buffer2)) {
fout.write(buffer2,0,c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
標簽:android,exception,zip
來源: https://codeday.me/bug/20190826/1728495.html
總結
以上是生活随笔為你收集整理的android zip解压出错,Android:解压缩文件会引发数据错误或CRC错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android zip解压 速度,如何在
- 下一篇: Linux常用命令及其英文全称