开发者

Extracting UTF-16 encoded file from ZIP archive in Java

In the last section of the code I print what the Reader gives me. But its just bogus, where did I go wrong?

public static void read_impl(File file, String targetFile) {
    // Create zipfile input stream
    FileInputStream stream = new FileInputStream(file);
    ZipInputStream zipFile = new ZipInputStream(new BufferedInputStream(stream));

    // Im looking for a specific file/entry
    while (!zipFile.getNextEntry().getName().equals(targetFile)) {
        zipFile.getNextEntry();
    }

    // Next step in api requires a reader
    // The target file is a UTF-16 encoded text file
    InputStreamReader reader = new InputStreamReader(zipFile, Charset.forName("UTF-16"));

    // I cant make sense of what this print
    char buf[] = new char[1];
    while (re开发者_运维百科ader.read(buf, 0, 1) != -1) {
        System.out.print(buf);
    }
}


I'd guess that where you went wrong was believing that the file was UTF-16 encoded.

Can you show a few initial byte values if you don't decode them?


Your use of a char array is a bit pointless, though at first glance it should work. Try this instead:

int c;
while ((c = reader.read()) != -1) {
    System.out.print((char)c);
}

If that does not work either, then perhaps you got the wrong file, or the file does not contain what you think it does, or the console can't display the characters it contains.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜