[英]Create Zip-File in java without using byte[]
in my programm, I generate multiple CSV-Files which I want to publish on a Webpage as one .zip file. 在我的程序中,我生成了多个CSV文件,我希望将它们作为一个.zip文件发布在网页上。 For that I want to use Java 1.6 on the server.
为此,我想在服务器上使用Java 1.6。
At the moment, I can create a .csv File without problems. 目前,我可以轻松创建一个.csv文件。 Now I want to write the content of the BufferedWriter, I use to create the csv-File, to write directly into a Zip-File (without storing the csv File).
现在,我想写BufferedWriter的内容,用它来创建csv文件,直接写到Zip文件中(不存储csv文件)。
I found some tutorials like Creating zip archive in Java and http://www.mkyong.com/java/how-to-compress-files-in-zip-format/ And I want to do more or less the same in my Application, but I don't like the byte-Arrays. 我找到了一些教程,例如在Java中创建zip归档文件和http://www.mkyong.com/java/how-to-compress-files-in-zip-format/,并且我希望在我的应用程序中做更多或更少的事情,但我不喜欢字节数组。
Can I avoid this byte-Arrays? 我可以避免使用此字节数组吗?
You can use the new IO package in Java for working with Zip files. 您可以使用Java中的新IO软件包来处理Zip文件。
Zip File System Provider is provided from Java 7 onwards. 从Java 7开始提供Zip文件系统提供程序 。
Map<String, String> env = new HashMap<>();
env.put("create", "true");
// locate file system by using the syntax
// defined in java.net.JarURLConnection
URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path externalTxtFile = Paths.get("/codeSamples/zipfs/SomeTextFile.txt");
Path pathInZipfile = zipfs.getPath("/SomeTextFile.txt");
// copy a file into the zip file
Files.copy( externalTxtFile,pathInZipfile,
StandardCopyOption.REPLACE_EXISTING );
}
Its not possible since you are using OutStream. 由于您正在使用OutStream,因此不可能。 Because OutStreams are dealing with byte to write.
因为OutStreams正在处理要写入的字节。
Normally two ways are I/O present. 通常,I / O存在两种方式。 One is Stream based (Byte), another is Reader(Char) based.
一个基于流(字节),另一个基于读取器(字符)。 Zip only contains stream based implementation.
压缩仅包含基于流的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.