首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java中来自字符串的IFile

java中来自字符串的IFile
EN

Stack Overflow用户
提问于 2020-08-18 18:18:41
回答 1查看 56关注 0票数 0

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

代码语言:javascript
复制
Map<String, String> fileMap = new HashMap<>();
fileMap.put("test1.txt", "Content of test1");
fileMap.put("test2.txt", "Content of test2");

如何为每个条目创建IFile?

EN

回答 1

Stack Overflow用户

发布于 2020-08-18 18:53:49

关于你在评论中说的话,这应该能做好以下工作:

代码语言:javascript
复制
    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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63466569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档