title: Replacing a Zip file without unzipping in Java
body: <p>I have a zip file and I want to replace one file inside it with another file. So do not need to delete a zip entry just replace the file for the zip entry with another.</p>
  
  <p>Here is what I have tried. </p>
  
  <pre><code>public void replaceConfigurationFile(ZipFile zipFile, ZipOutputStream zos, String pathToNewFile, String configFileToReplaced) 
          throws IOException {
      String zipEntryName;
      for(Enumeration&lt;?&gt; e = zipFile.entries(); e.hasMoreElements(); ) {
  
          ZipEntry entryIn = (ZipEntry) e.nextElement();
          zipEntryName = entryIn.getName();
              if(zipEntryName.endsWith(configFileToReplaced)) {
  
                  FileInputStream fis = new FileInputStream(pathToNewFile);
                  ZipEntry zipEntry = new ZipEntry(zipEntryName);
  
                  zos.putNextEntry(zipEntry);
  
                  byte[] bytes = new byte[1024];
                  int length;
                  while ((length = fis.read(bytes)) &gt;= 0) {
                      zos.write(bytes, 0, length);
                  }
  
                  zos.closeEntry();
                  fis.close();
              } else {
                  zos.putNextEntry(entryIn);
                  InputStream is = zipFile.getInputStream(entryIn);
                  byte [] buf = new byte[1024];
                  int len;
                  while((len = (is.read(buf))) &gt; 0) {            
                      zos.write(buf, 0, len);
                  }
               zos.closeEntry();  
             }
  
  
      } // enf of for
  }   
  </code></pre>
  
  <p>I have a file zip Entry named :</p>
  
  <pre><code>WEB-INF/classes/config/app-dev.yml
  </code></pre>
  
  <p>and I have a file at location d drive at location</p>
  
  <pre><code> D:/app-dev.yml
  </code></pre>
  
  <p>I am able to copy the file in to a different zip file by replacing the file i want to replace . But that is really not needed (to create a different file).
  So what should I do to just replace the file with my custom file.</p>
  
  <p>I have searched different posts in Stackoverflow, but unable to find what i need. I read that zip entry cannot be deleted but what about replacing it ? Please help</p>