0

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?

Community
  • 1
  • 1
waXve
  • 792
  • 2
  • 9
  • 30
  • 2
    What's your problem with byte arrays? What's your problem? – user207421 Aug 18 '14 at 09:59
  • If I understood your question, you want to create a zip without an existing file on the filesystem. [This question](http://stackoverflow.com/questions/1091788/how-to-create-a-zip-file-in-java) shows how to create a zip file by directly writing a in-memory string to a zip entry (so you don't have to zip a file stored in the file system). And no, you can't skip byte arrays. – BackSlash Aug 18 '14 at 10:03

2 Answers2

1

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 ); 
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
dnavz28
  • 11
  • 1
0

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.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26