开发者

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. 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 ", conten开发者_Python百科tIntent);
            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..

Original file size: 3.67mb .. Download file size: 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜