title: Download and save a zip file from httppost response as binary data in ANDROID
body: <p>Thanks in advance..</p>
  
  <p>First, I'm asking this for android.</p>
  
  <p>I have to send a http post request with a zip file containing an xml file having list of names in it.</p>
  
  <p>now, according to the list of name I sent, the server will send me the binary data of a zip file and I have to save that binary data (response) as a zip file.</p>
  
  <p>the problem is, when I'm saving this binary data as a zip file, it is not able to extract the zip then.</p>
  
  <p>I think this could be some character set problem too.. that i need to convert the received binary data to some character set and then save it as a zip..</p>
  
  <p>please Help me in this, I'm new to android. and any ASYNC task example for doing the same will be great Help.</p>
  
  <p>This is my code..</p>
  
  <pre><code>private class sendMissingImagesToServer extends
          AsyncTask&lt;String, Integer, byte[]&gt; {
  
      @Override
      protected byte[] doInBackground(String... params) {
          String uri = params[0];
          try {
  
              MultipartEntityBuilder entity;
              File f;
              FileBody fb;
              entity = MultipartEntityBuilder.create();
  
              entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
              f = new File(zipImageFile);
              fb = new FileBody(f);
              entity.addPart("orderFile", fb);
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(uri);
              Log.e("Uploload Missing Image URL", "" + uri);
              httppost.setEntity(entity.build());
              HttpResponse response = httpclient.execute(httppost);
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
              StringBuffer stringBuffer = new StringBuffer();
  //              byte[] fileBites=null;
              String line = "";
  
              while ((line = bufferedReader.readLine()) != null) {
                  stringBuffer.append(line);
              }
              bufferedReader.close();
  
  //              fileBites=stringBuffer.toString().getBytes();
  //              Log.e("FILE BITES", fileBites+"=&gt;"+fileBites.length);
  
              ByteArrayOutputStream bObj = new ByteArrayOutputStream();
              bObj.reset();
              bObj.write(stringBuffer.toString().getBytes());
  
              return bObj.toByteArray();
  
  //              return stringBuffer.toString();
          } catch (Exception e) {
              return e.toString().getBytes();
          }
  
      }
  
      @Override
      protected void onPostExecute(byte[] result) {
          // TODO Auto-generated method stub
          super.onPostExecute(result);
          Log.e("Response From Server", "" + result);
          writeToFile(result);
  
      }
  
  }
  
  @SuppressWarnings("resource")
  private void writeToFile(byte[] data) {
      try {
  
          FileOutputStream fop = null;
          File file;
  
          file = new File(AppConstants.DataPath+"/products.zip");
          fop = new FileOutputStream(file);
  
          // if file doesnt exists, then create it
          if (!file.exists()) {
              file.createNewFile();
          }
          try {            
          fop.write(data);
  
      } catch (IOException e) {
          Log.e("Exception", "File write failed: " + e.toString());
      }
      unzipImage(AppConstants.DataPath + "/products.zip",
              AppConstants.DataPath);
  }catch (Exception E)
  {
  
  }
  }
  </code></pre>