我有一个包含文件名和文件内容的Map。我想为映射中的每个条目创建IFile。
Map<String, String> fileMap = new HashMap<>();
fileMap.put("test1.txt", "Content of test1");
fileMap.put("test2.txt", "Content of test2");如何为每个条目创建IFile?
发布于 2020-08-18 18:53:49
关于你在评论中说的话,这应该能做好以下工作:
Map<String, String> fileMap = new HashMap<>();
fileMap.put("test1.txt", "Content of test1");
fileMap.put("test2.txt", "Content of test2");
FileOutputStream fos = new FileOutputStream("multiCompressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
for (Map.Entry<String, String> file : fileMap.entrySet()) {
File fileToZip = new File(file.getKey());
FileWriter writer = new FileWriter(fileToZip);
writer.write(file.getValue());
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
zipOut.close();
fos.close();
}https://stackoverflow.com/questions/63466569
复制相似问题