[英]IFile from String in java
I have a Map<String, String> containing the name of the file and content of the file.我有一个 Map<String, String> 包含文件名和文件内容。 I want to create IFile for each of the entry in the map.
我想为 map 中的每个条目创建 IFile。
Map<String, String> fileMap = new HashMap<>();
fileMap.put("test1.txt", "Content of test1");
fileMap.put("test2.txt", "Content of test2");
How do I create IFile for each of the entries?如何为每个条目创建 IFile?
Regarding what you said in your comments, this should do the work:关于您在评论中所说的内容,这应该可以完成工作:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.