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?

2
  • 2
    What's your problem with byte arrays? What's your problem? Commented Aug 18, 2014 at 9:59
  • If I understood your question, you want to create a zip without an existing file on the filesystem. This question 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. Commented Aug 18, 2014 at 10:03

2 Answers 2

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 ); 
}
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

That's not the answere I want to hear... OK so I have to handle with the bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.