Android:Zip是从服务器下载的,没有正确解压缩

时间:2014-03-08 13:12:31

标签: android http zip unzip

我们正准备一个应用程序,我们必须从服务器上下载一堆数据。 所以,我们决定制作整个数据的Zip并从服务器下载..

它也可以正常工作..我们可以完全下载文件...

我的问题是:

(1)我们的文件已完全下载,但显示Archieve为未知格式或已损坏。 如何下载文件而不会在Android中使用HttpConnection受到损坏.. ??

(2)如果我们使用WinRar Tools(Alt+R)修复已下载的文件,那么新的Zip工作正常.. 所以,我的问题是如何在Android中以编程方式修复zip文件.. ??

从服务器下载zip的代码:

HttpURLConnection connection = null;

URL url = null;

try
{
    url = new URL(zipPath);
    connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(connectionTimeOutSec * 1000);
    connection.setReadTimeout(connectionReadOutSec * 60 * 1000);
    connection.setInstanceFollowRedirects(false);
}
catch (MalformedURLException e)
{

        e.printStackTrace();
        return "";

} catch (IOException e)
{
    e.printStackTrace();
    return "";
}

if(firsttime == 0)
{
    firsttime = 1;

            if(file.exists()){

                 downloaded = (int) file.length();
                 connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
            );
            }
            else
            {
                file.mkdirs();
                file.delete();
            }

        }
else {

            connection.setRequestProperty("Range", "bytes=" + (file.length()) + "-");

        }

        connection.setDoInput(true);
        connection.setDoOutput(true);

        if(file.exists()){

            TotalByes = (int) file.length();
       }

        file = null;
        Log.d("Total Value Received", "" + connection.getContentLength());

        if(connection.getContentLength() > 0)
        TotalByes += connection.getContentLength();

        try {

            in = new BufferedInputStream(connection.getInputStream());
            fos = (downloaded == 0)? new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/EduTab"+"/"+zipfilePath): new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/EduTab"+"/"+zipfilePath,true);
        }
         catch (IOException e) {

            if(connection != null)
            {
                connection.disconnect();    
            }
            e.printStackTrace();
            return "";
        }

        bout = new BufferedOutputStream(fos, 1024);
        byte[] data = new byte[1024];


        int x = 0;
        try {
            while ((x = in.read(data, 0, 1024)) >= 0) {

                 bout.write(data, 0, x);

                 downloaded += x;

            }

        } catch (IOException e) {

            if(connection != null)
            {
                connection.disconnect();    
            }

            e.printStackTrace();

            return "";
        }

对于下载Zip文件,我们已经引用此链接:

Resume http file download in java

任何进一步的帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

终于得到了答案..

实际上文件没有在SD卡上正确保存..

其背后的原因是BufferedOutputStream object "bos" 未在代码末尾关闭,因为 某些字节未添加到实际文件中..和Zip在提取它时显示已损坏...

刚刚在while循环结束时添加了bos.close()

无需调用bos.flush(),因为close() function会自动将字节刷新到实际关闭BufferedOutputStream ..

之前的文件