简体   繁体   中英

IFile from String in java

I have a Map<String, String> containing the name of the file and content of the file. I want to create IFile for each of the entry in the map.

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM