在CSV文件创建方面,以下代码可以正常工作(仅在下面显示相关代码):
rsCompany = pstmtCompany.executeQuery();
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
Path filecompany = dir.resolve("company_custom_file_" + unixTimestamp + ".csv");
try (CSVWriter writer = new CSVWriter(Files.newBufferedWriter(filecompany))) {
writer.writeAll(rsCompany, true);
}
现在,假设我要为此创建一个zip文件,我应该如何在此行dir
中使用FileOutputStream fos = new FileOutputStream("your_files.zip");
变量(在上述场景中使用的变量)在下面?
我的意思是,我必须定义以下内容:
Path dir = Paths.get("/srv/custom_users", userName);
Files.createDirectories(dir);
FileOutputStream fos = new FileOutputStream("your_files.zip");
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry(file.getFileName().toString());
zos.putNextEntry(entry);
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(zos,StandardCharsets.UTF_8)))) {
writer.writeAll(rsDemo, true);
writer.flush();
zos.closeEntry();
}
zos.close();
如果我继续使用newOutputstream
类的Files
method,它可能如下所示:
FileOutputStream fos = new FileOutputStream(Files.newOutputStream(dir));
这是正确的方法吗?
我想知道应该在上一行代码your_files.zip
中定义的FileOutputStream("your_files.zip");
压缩文件的名称放在哪里?
答案 0 :(得分:2)
您不需要FileOutputStream。完全没有。
您只需要一个OutputStream,这就是Files.newOutputStream返回的内容:
OutputStream fos = Files.newOutputStream(dir.resolve("your_files.zip"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
ZipOutputStream zos = new ZipOutputStream(bos);