anton
anton

Reputation: 27

creating a zip archive with txt files with content having utf-8 character set

I have several strings (Json objects) that I want to write to a zip archive. The content should have UTF-8 character set. I could first create txt files on the hard disk and then zip them together. But is it also possible to create a zip archive directly with the txt files and the utf-8 character set?

At the moment I'm writing the bytes of the strings directly into the archive. And get the system's character set.

String str = "...";

ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream("dest\\zipFile.zip"));

zipOutputStream.putNextEntry(new ZipEntry("entryName"));
zipOutputStream.write(str.getBytes());

zipOutputStream.close();

Upvotes: 1

Views: 403

Answers (1)

A Le Dref
A Le Dref

Reputation: 452

what your doing seems right to me.

If you want to right multiple files then call .closeEntry(), and then you can add a new file using putNextEntry and write and as entry name use your file name.

For character encoding you could use something like this:

    ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(outputFile));
    BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(zipStream, Charset.forName("YOUR_WANTED_CHARSET"))
    );

Upvotes: 1

Related Questions