2017-11-16 8 views
1

異なる場所にあるさまざまな種類のファイル(画像、フォントなど)を含む、Javaでzipファイルを生成しようとしています。さらに、zipファイルには、ファイルをタイプ別に置くサブフォルダが必要です(イメージは、zip内のimagesフォルダに移動する必要があります)。Java - サブフォルダ別の場所にある複数のファイルでZipファイルを作成する

これらのファイルは、それぞれ別の場所に置くことができます。 :

  • index.htmlを
  • img1.jpg
  • img2.jpg
  • font1.woff
  • font2.woff style.cssに
  • custom.js

そして、これが、彼らはzipファイルにどうあるべきかです:

  • index.htmlを
  • 画像/ img1.jpg
  • 画像/ IMG2 .jpg
  • フォント/ font1.woff
  • フォント/ font2.woff
  • 軒のjsが/
  • custom.js CSS/Styles.cssを

は、これまでのところ私は、特定のパスで一つのファイルを取り、出力場所のためにユーザを促すために管理しています。 zipファイルは、入力に指定されたファイルとともに生成されます。ここで私が持っているコードは、これまでのところです:

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)) > 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) {} 
    } 
} 

誰かが私を助けることができる場合、私は本当に喜んでいるだろう!

答えて

0

このように動作するはずです。

zipディレクトリ名は、別の方法で作成する必要があります(jpgよりも多くのイメージタイプがあります:)。

public static Path zip(List<Path> files, Path zipFileTarget) throws IOException { 
    try (FileOutputStream fos = new FileOutputStream(zipFileTarget.toFile()); 
     ZipOutputStream zos = new ZipOutputStream(fos)) { 
     if (!Files.exists(zipFileTarget)) 
      Files.createFile(zipFileTarget); 
     createEntries(files, zos); 
     zos.close(); 
     return zipFileTarget; 
    } 
} 

private static List<String> createEntries(List<Path> files, ZipOutputStream zos) throws IOException { 
    List<String> zippedFiles = new ArrayList<>(); 
    Matcher matcherFileExt = Pattern.compile("^.*\\.([^.]+)$").matcher(""); 
    for (Path f : files) { 
     if (Files.isRegularFile(f)) { 
      String fileName = f.getFileName().toString(); 
      String fileExt = matcherFileExt.reset(fileName).matches() 
        ? matcherFileExt.replaceAll("$1") 
        : "unknown"; 
      // You should determine the dir name with a more sophisticated 
      // approach. 
      String dir; 
      if  (fileExt.equals("jpg")) dir = "images"; 
      else if (fileExt.equals("woff")) dir = "fonts"; 
      else dir = fileExt; 

      zos.putNextEntry(new ZipEntry(dir + "/" + fileName)); 
      Files.copy(f, zos); 
      zippedFiles.add(fileName); 
     } 
    } 
    return zippedFiles; 
} 

編集:このアプローチは、Java 1.7以降で動作します。 toPath()メソッドを呼び出すことで、FileオブジェクトをPathオブジェクトに簡単に変換できます。

関連する問題