Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:Zip是从服务器下载的,未正确解压缩_Android_Http_Zip_Unzip - Fatal编程技术网

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

Android:Zip是从服务器下载的,未正确解压缩,android,http,zip,unzip,Android,Http,Zip,Unzip,我们正在准备一个应用程序,其中我们必须从服务器下载大量数据。。 所以,我们决定对整个数据进行压缩,并从服务器下载 而且它也很好用。。我们能够完全下载文件 我的问题是: 1我们的文件已完全下载,但显示Archieve的格式未知或已损坏。如何在Android中使用HttpConnection下载文件而不被损坏 2如果我们使用WinRar工具SALT+R修复下载的文件,那么新的Zip将正常工作。。 所以,我的问题是如何在Android中以编程方式修复zip文件 从服务器下载zip的代码: 下载Zip文

我们正在准备一个应用程序,其中我们必须从服务器下载大量数据。。 所以,我们决定对整个数据进行压缩,并从服务器下载

而且它也很好用。。我们能够完全下载文件

我的问题是:

1我们的文件已完全下载,但显示Archieve的格式未知或已损坏。如何在Android中使用HttpConnection下载文件而不被损坏

2如果我们使用WinRar工具SALT+R修复下载的文件,那么新的Zip将正常工作。。 所以,我的问题是如何在Android中以编程方式修复zip文件

从服务器下载zip的代码:

下载Zip文件时,我们参考了以下链接:


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

终于得到了答案

实际上,文件未正确保存在SD卡上。。 这背后的原因是BufferedOutputStream对象bos没有在代码末尾关闭,因为有些字节没有添加到实际文件中。。解压缩时拉链显示损坏

刚刚在while循环的末尾添加了bos.close。。无需调用bos.flush,因为关闭函数会在关闭BufferedOutputStream之前自动将字节刷新到实际文件中

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 "";
        }