문제

We are Preparing a Application in which we have to download a bunch of data from Server.. So, We have decided to make a Zip of the whole data and download it from server..

And it works fine also.. We are able to download File completely...

My Question Are:

( 1 ) Our File is getting downloaded fully but it shows Archieve is Either in Unknown Format or Damaged. How to download file without getting Damaged Using HttpConnection in Android..??

( 2 ) If we repair Downloaded file using WinRar Tools(Alt+R) then the new Zip works properly.. So, my Question is How to Repair a zip File Programmatically in Android..??

Code for Downloading zip from Server:

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

For Downloading Zip File we have referred this link:

Resume http file download in java

Any further help would be appreciated..

도움이 되었습니까?

해결책

Finally Got the answer..

Actually file was not getting properly saved on SDCard..

The Reason behind that was BufferedOutputStream object "bos" was not closed at the end of the Code, because of that some bytes where not added to the actual file.. and the Zip showed corrupted while extracting it...

Just added bos.close() at the end of the while loop..

No need to call bos.flush(), As the close() function automatically flush the bytes to the actual file before closing the BufferedOutputStream..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top