Android:解压缩文件会引发数据错误或CRC错误

Android:解压缩文件会引发数据错误或CRC错误,第1张

概述我正在开发一个下载zip文件并在本地解压缩的项目.我遇到的问题是解压缩过程在5%的时间内起作用.在这一点上,这对我来说是一个谜,因为有时它可以工作,但大多数时候它会抛出数据或crc错误.即使zip文件没有改变,它甚至会在错误之间切换.我尝试过由众多工具创建的zip文件,想知道格式是

我正在开发一个下载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.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.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 databyte[] 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:解压缩文件会引发数据错误或CRC错误全部内容,希望文章能够帮你解决Android:解压缩文件会引发数据错误或CRC错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1109833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存