Java中的String IFile

问题描述

我有一个Map ,其中包含文件名和文件内容。我想为地图中的每个条目创建IFile。

Map<String,String> fileMap = new HashMap<>();
fileMap.put("test1.txt","Content of test1");
fileMap.put("test2.txt","Content of test2");

如何为每个条目创建IFile?

解决方法

关于您在评论中所说的,这应该可以完成工作:

    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,length);
        }
        fis.close();
    }
    zipOut.close();
    fos.close();
}