Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:12:44+00:00 2026-06-01T14:12:44+00:00

I try to download a zip file to android and then extract the zip

  • 0

I try to download a zip file to android and then extract the zip file. I debug my code and there is a funny problem: The downloaded zip file is a bit larger than origin file. And the downloaded zip file can’t be unziped by winrar. It is said that the downloaded file is ended with error. (The zip file on my website is OK. I try to download with IE. It works fine.)
Following is my code:

public void download(final String url, final String savePath, final String saveName) {
    new Thread(new Runnable() {
        public void run() {
            try {
                sendMessage(FILE_DOWNLOAD_CONNECT);
                URL sourceUrl = new URL(url);
                URLConnection conn = sourceUrl.openConnection();
                conn.connect();
                InputStream inputStream = conn.getInputStream();

                int fileSize = conn.getContentLength();

                File savefilepath = new File(savePath);
                if (!savefilepath.exists()) {
                    savefilepath.mkdirs();
                }
                File savefile = new File(savePath+saveName);
                if (savefile.exists()) {
                    savefile.delete();
                }
                savefile.createNewFile();

                FileOutputStream outputStream = new FileOutputStream(
                    savePath+saveName, true);

                byte[] buffer = new byte[1024];
                int readCount = 0;
                int readNum = 0;
                int prevPercent = 0;
                while (readCount < fileSize && readNum != -1) {
                    readNum = inputStream.read(buffer);
                    if (readNum > -1) {
                        outputStream.write(buffer);

                        readCount = readCount + readNum;

                        int percent = (int) (readCount * 100 / fileSize);
                        if (percent > prevPercent) {
                            sendMessage(FILE_DOWNLOAD_UPDATE, percent,
                                readCount);

                            prevPercent = percent;
                        }
                    }
                }
                outputStream.flush();
                outputStream.close();
                inputStream.close();
                //Thread.sleep(50);
                sendMessage(FILE_DOWNLOAD_COMPLETE, savePath);

            } catch (Exception e) {
                sendMessage(FILE_DOWNLOAD_ERROR, e);
            }
        }
    }).start();
}

Anybody know this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:12:45+00:00Added an answer on June 1, 2026 at 2:12 pm

    When you read into the buffer, you are writing the entire buffer to the output stream:

    outputStream.write(buffer);
    

    You should only write the part of the buffer that was filled:

    outputStream.write(buffer, 0, readNum);
    

    Particularly with network downloads, there is no guarantee that a call to inputStream.read(buffer) will fill the buffer (even if there is more than 1024 bytes to go in the file).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my PHP code I try to download a file from a URL starting
I try to delete file after download file.My code is private void DownloadZipFileDialogue(string strZipFilePath)
I am trying to download multiple PDF files as one zip file and then
This is my C# code to download a ZIP file from my server. When
I'm trying to make android download a zip file from my web server using
I have a big problem dealing with data I try to download in my
i'm download DrillDownApp example project from iPhoneSDKArticles i have a problem when i'm try
When I have download sample code from this link and than try to build
I am using the api twitterizer2 downloaded from http://code.google.com/p/twitterizer/downloads/list but when I try to
I am trying to download a zip file using HttpCLient 4 and it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.