異なる場所にあるさまざまな種類のファイル(画像、フォントなど)を含む、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) {}
}
}
誰かが私を助けることができる場合、私は本当に喜んでいるだろう!