开发者

Zipping a byte[] from a txt file

I need to read a txt file, convert it from UTF8 to ISO8859-1 and store the textfile into a zip.

This is what i got so far:

Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-1");

File file_in = new File("Input/file1.txt");
File file_out = new File("Output/file_out.txt");

try {
    FileInputStream fis = new FileInputStream(file_in);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte fileContent[] = new byte[(int)file_in.length()];

    bis.read(fileContent);

    ByteBuffer bb = ByteBuffer.wrap(fileContent);
    CharBuffer data = utf8charset.decode(bb);
    ByteBuffer outputBuffer = iso88591charset.encode(data);
    byte outputData[] = outputBuffer.array();

    FileOutputStream fos = new FileOutputStream(file_out);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(outp开发者_C百科utData);
    bos.close();

} catch ...

Do I have to create the txt file, read it again and zip it den with ZipOutputStream? Or is there a way to use the byte[] of the txtfile to create the zip??


Short answer is yes, you can write the zip directly from the byte array.

Look at the documentation for: java.util.zip.ZipOutputStream.write(). It takes a byte array, starting offset, and length as it's parameters.

Should be self explanatory from there.

And just to warn you, not all UTF-8 can be encoded as ISO8859-1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜