Question

i have a created 3 xml files and compressed into a zip folder . The folder is send from server. When i download the zip folder through browser, its working properly and can extract the files. But when i download it from android application and store in SD card, it is corrupted. I pulled the file from SD card to computer and tried to extract the folder, it shows Zip Folder is invalid . My code is given below :

DefaultHttpClient httpclient1 = new DefaultHttpClient();
                HttpPost httpPostRequest = new HttpPost(
                        Configuration.URL_FEED_UPDATE);
                            byte[] responseByte = httpclient1.execute(httpPostRequest,
                        new BasicResponseHandler()).getBytes();

                InputStream is = new ByteArrayInputStream(responseByte);


                // ---------------------------------------------------

                File file1 = new File(Environment
        .getExternalStorageDirectory() + "/ast");
                file1.mkdirs();
                //
                 File outputFile = new File(file1, "ast.zip");
                FileOutputStream fos = new FileOutputStream(outputFile);

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();                
                is.close();

When I used

ZipInputStream zin = new ZipInputStream(new BufferedInputStream(is));

The ZipInputStream can't store values from stream.

Was it helpful?

Solution

I'd guess that your main mistake is where you get the input stream. What you are actually doing is to get the server response as String (BasicResponseHandler) and then converting that to bytes again. Since Java is all UTF-8 this most likely does not work.

Better try something like

HttpResponse response = httpclient1.execute(httpPostRequest);
InputStream is = response.getEntity().getContent()

(And do better null pointer checking, read the content in a try-catch block and make sure you close all resources in a finally block.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top