简体   繁体   English

文件下载代码下载的文件大于原始文件

[英]File download code downloads files larger than original

I'm downloading some .zip files though when I try to unzip them, I get "data error".. Now I went and saw the downloaded files, and they are bigger than the original. 我正在下载一些.zip文件,但是当我尝试解压缩它们时,出现“数据错误”。。现在,我去看了下载的文件,它们比原始文件大。 Could this be the reason of the error? 这可能是错误的原因吗?

Code to download the file: 下载文件的代码:

URL=intent.getStringExtra("DownloadService_URL");
    FileName=intent.getStringExtra("DownloadService_FILENAME");
    Path=intent.getStringExtra("DownloadService_PATH");

    try{


    URL url = new URL(URL);
    URLConnection conexion = url.openConnection();

    conexion.connect();
    int lenghtOfFile = conexion.getContentLength();
    lenghtOfFile/=100;

    InputStream input = new BufferedInputStream(url.openStream());
    OutputStream output = new FileOutputStream(Path+FileName);

    byte data[] = new byte[1024];
    long total = 0;

    int count = 0;
    while ((count = input.read(data)) != -1) {
        output.write(data);
        total += count;

        notification.setLatestEventInfo(context, contentTitle, "Starting download " + FileName + " " + (total/lenghtOfFile), contentIntent);
        mNotificationManager.notify(1, notification);
    }


    output.flush();
    output.close();
    input.close();

Code to UnZip: 要解压缩的代码:

try {

            String zipFile = Path + FileName;


            FileInputStream fin = new FileInputStream(zipFile);
            ZipInputStream zin = new ZipInputStream(fin);

            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                UnzipCounter++;
                if (ze.isDirectory()) {
                    dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(Path
                            + ze.getName());
                    while ((Unziplength = zin.read(Unzipbuffer)) > 0) {
                        fout.write(Unzipbuffer, 0, Unziplength);                    
                    }
                    zin.closeEntry();
                    fout.close();

                }

            }
            zin.close();
            File f = new File(zipFile);
            f.delete();
            notification.setLatestEventInfo(context, contentTitle, "File successfully downloaded", contentIntent);
            mNotificationManager.notify(1, notification);

        } catch (Exception e) {

            notification.setLatestEventInfo(context, contentTitle, "Problem in downloading file ", contentIntent);
            mNotificationManager.notify(1, notification);

        }

    }

The unzip proccess starts but stops after extracting some files with that error.. I tried anothe r .zip file and I got CRC Failed error.. I tested both .zip files with winrar.. 解压缩过程开始但在解压缩带有该错误的某些文件后停止。.我尝试了anothe r .zip文件,但收到CRC Failed错误。.我用winrar测试了这两个.zip文件。

Original file size: 3.67mb .. Download file size: 3.93mb 原始文件大小:3.67mb ..下载文件大小:3.93mb

You always write the complete byte array to the disk without checking how much data you read in. 您总是将完整的字节数组写入磁盘,而无需检查读取的数据量。

Also from a performance point of view anything <1500byte (ie usual ethernet MTU) is a pretty bad idea - though I think Java buffers that somewhere below anyhow, but why risk anything. 从性能的角度来看,任何小于1500byte(即普通的以太网MTU)的想法都不是个好主意-尽管我认为Java会将其缓冲在任何下面,但是为什么要冒任何风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM