[英]How can I make my Java-generated zip file identical to a WinZip-generated one
[英]How to zip a file that is generated in the code (Java)
好了,我正在生成2個.xml文件,但是我想將它們保存在一個文件夾中,也要對其進行壓縮,我不想分別生成它們,而是將它們分開放在一個文件夾中並壓縮。 d你明白了嗎?
我想將要創建的兩個文件都放入一個文件夾並壓縮。 我唯一要保存的是zip文件。
String name = fields.find(chooser.getLocation(), "mimic");
Mimic mimic = getMimic(mimicList.get(0));
String fileName = chooser.getSelectedFile().toString() + File.separator + "des_" +nombreMimic.substring(0, nombreMimic.length()-4)+ ".xml";
String config = chooser.getSelectedFile().toString() + File.separator + "cfg_" +nombreMimic.substring(0, nombreMimic.length()-4)+".xml";
FileOutputStream file;
FileOutputStream file2;
file = new FileOutputStream(fileName);
file2 =new FileOutputStream(config);
Parser parser;
parser = new Parser(file,new String[]{});
parser.render(mimic , fields);
JOptionPane.showMessageDialog(null, "Complete!");
Parser2 parser2;
parser2 = new Parser2(file2,new String[]{});
parser2.render(mimic , fields);
JOptionPane.showMessageDialog(null, "Complete!");
FileInputStream inputStream = new FileInputStream(chooser.getSelectedFile().toString() + File.separator + "des_" +nombreMimic.substring(0, nombreMimic.length()-4)+ ".xml");
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(nombreMimic.substring(0, nombreMimic.length()-4)+".des"));
zip.putNextEntry(new ZipEntry(fileName));
byte [] buffer = new byte [1024];
int bytesRead;
while((bytesRead=inputStream.read(buffer))>0){
zip.write(buffer,0,bytesRead);
}
zip.closeEntry();
zip.close();
inputStream.close();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Error parsing");
}
}
}
}
您的問題的答案似乎在這里得到了答案: 如何使用Java壓縮文件夾本身
在那里說明了如何使用Java壓縮文件/文件夾。
您必須將文件保存在文件夾中才能正常工作,但是一旦將其壓縮,就可以按照本教程使用以下內容刪除原始文件夾:
String path = "/path/to/file";
Files.delete(path);
這樣,唯一保存的將是zip文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.