title: Base64-encode a file and compress it
body: <p>My goal is to encode a file and zip it in a folder in java. I have to use the Apache's Commons-codec library. I am able to encode and zip it and it works fine but when i decode it back to its original form, it looks like the file has not completely been encoded. Looks like a few parts are missing. Can anybody tell me why this happens?</p>
  
  <p>I am also attaching the part of my code for your reference so that you can guide me accordingly.</p>
  
  <pre><code>private void zip() {
      int BUFFER_SIZE = 4096;
      byte[] buffer = new byte[BUFFER_SIZE];
  
      try {
          // Create the ZIP file
          String outFilename = "H:\\OUTPUT.zip";
          ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                  outFilename));
  
          // Compress the files
          for (int i : list.getSelectedIndices()) {
              System.out.println(vector.elementAt(i));
              FileInputStream in = new FileInputStream(vector.elementAt(i));
              File f = vector.elementAt(i);
  
              // Add ZIP entry to output stream.
              out.putNextEntry(new ZipEntry(f.getName()));
  
              // Transfer bytes from the file to the ZIP file
              int len;
  
              while ((len = in.read(buffer)) &gt; 0) {
                  buffer = org.apache.commons.codec.binary.Base64
                          .encodeBase64(buffer);
                  out.write(buffer, 0, len);
  
              }
  
              // Complete the entry
              out.closeEntry();
              in.close();
  
          }
  
          // Complete the ZIP file
          out.close();
      } catch (IOException e) {
          System.out.println("caught exception");
          e.printStackTrace();
      }
  }
  </code></pre>