简体   繁体   English

在Java中创建Zip文件而不使用byte []

[英]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.

相关问题 Java:递归地将文件添加到zip文件但没有完整路径 - Java: Add files to zip-file recursively but without full path Java-使用子文件夹从不同位置创建包含多个文件的Zip文件 - Java - Create Zip-file with multiple files from different locations with subfolders 如何从客户端计算机下载zip文件,而不是使用Java在浏览器中打开文件? - how to download zip-file from client machine instead of opening file in browser using java? 使用 Java 从 S3 并行下载多个文件到一个 zip 文件 - Download multiple files in parallel to a zip-file from S3 using Java 解压缩后打开/下载zip文件时出现问题(Java / ZipInputStream) - Problem opening/downloading zip-file after decompression (Java/ZipInputStream) 在名为zip-file的文件夹中使用骆驼解压缩文件 - Unzip file using camel in folder named like zip-file 如何使用 Liferay 将多个文件下载为 zip 文件? - How to download multiple files as a zip-file using Liferay? 为什么我在使用java.util.zip.ZipFile打开一个空的ZIP文件时遇到异常? - Why I get an Exception on opening an empty ZIP-file with java.util.zip.ZipFile? 播放压缩文件中的音频文件 - Playing audio files packed in zip-File 将家长文档包含到zip文件中 - Include parents documentation into zip-file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM