简体   繁体   中英

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 (reader.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM