waXve
waXve

Reputation: 862

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. For that I want to use Java 1.6 on the server.

At the moment, I can create a .csv File without problems. 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).

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.

Can I avoid this byte-Arrays?

Upvotes: 0

Views: 820

Answers (2)

dnavz28
dnavz28

Reputation: 11

You can use the new IO package in Java for working with Zip files.

Zip File System Provider is provided from Java 7 onwards.

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

Upvotes: 1

Siva Kumar
Siva Kumar

Reputation: 2006

Its not possible since you are using OutStream. Because OutStreams are dealing with byte to write.

Normally two ways are I/O present. One is Stream based (Byte), another is Reader(Char) based. Zip only contains stream based implementation.

Upvotes: 0

Related Questions