title: Java - Create Zip-file with multiple files from different locations with subfolders
body: <p>I am trying to generate a zip file in Java, that contains several files of different types (e.g. images, fonts etc) that are lying in different locations. Furthermore I want the zip file to have subfolders where the files are put by their type (e.g. images should go to the images folder within the zip.</p>
  
  <p>These are the files that I have (each can be in a different location):</p>
  
  <ul>
  <li>index.html</li>
  <li>img1.jpg</li>
  <li>img2.jpg</li>
  <li>font1.woff</li>
  <li>font2.woff</li>
  <li>style.css</li>
  <li>custom.js</li>
  </ul>
  
  <p>And this is how they should be in the zip file:</p>
  
  <ul>
  <li>index.html</li>
  <li>images/img1.jpg</li>
  <li>images/img2.jpg</li>
  <li>fonts/font1.woff</li>
  <li>fonts/font2.woff</li>
  <li>js/custom.js</li>
  <li>css/styles.css</li>
  </ul>
  
  <p>So far I have managed to take one file in a specific path and prompt the user for the output location. A zip-file will be generated with the file that is specified in the input. Here is the code I have so far:</p>
  
  <pre><code>JFrame parentFrame = new JFrame();
  
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.setDialogTitle("Speicherort auswählen");   
  
  int userSelection = fileChooser.showSaveDialog(parentFrame);
  String pathToFile;
  
  if (userSelection == JFileChooser.APPROVE_OPTION) {
      File fileToSave = fileChooser.getSelectedFile();
      print(fileToSave.getAbsolutePath());
      pathToFile = fileToSave.getAbsolutePath();
  }
  
  pathToFile = pathToFile.replace("\\", "/");
  
  String outFileName = pathToFile;
  String inFileName = "C:/Users/asoares/Desktop/mobio_export_test/index.html";
  ZipOutputStream zos = null; 
  FileInputStream fis = null;
  
  try {
      zos = new ZipOutputStream(new FileOutputStream(outFileName));
      fis = new FileInputStream(inFileName);
      zos.putNextEntry(new ZipEntry(new File(inFileName).getName()));
      int len;
      byte[] buffer = new byte[2048];
      while((len = fis.read(buffer, 0, buffer.length)) &gt; 0) {
          zos.write(buffer, 0, len);
      }
  } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
  } catch (IOException e) { 
      e.printStackTrace(); 
  } finally { 
      if(fis != null){ 
          try { 
              fis.close(); 
          } catch (IOException e) {} 
      } 
      if(zos != null){ 
          try { 
              zos.closeEntry(); 
              zos.close(); 
          } catch (IOException e) {} 
      } 
  }
  </code></pre>
  
  <p>I would be really glad if someone can help me!!!</p>