简体   繁体   English

如何使用 ZipInputStream 解决此编码问题?

[英]How do I resolve this encoding problem with ZipInputStream?

I'm doing a ZipInputStream request on a UTF-8 encoded zip file.我正在对 UTF-8 编码的 zip 文件执行 ZipInputStream 请求。

I get the data through OK, but special German characters are coming out wrong.我通过 OK 获得数据,但特殊的德语字符出现错误。

Using this page ( http://kellykjones.tripod.com/webtools/ascii_utf8_table.html ) I can see that my code is printing out the two individual chars from the UTF8 encoding column.使用此页面( http://kellykjones.tripod.com/webtools/ascii_utf8_table.html )我可以看到我的代码正在打印出 UTF8 编码列中的两个单独的字符。

ie ä is UTF 0xC3,0xA4, and I am getting ä printed out (which are the 0xC3 and 0xA4 chars).即 ä 是 UTF 0xC3,0xA4,我得到 ä 打印出来(即 0xC3 和 0xA4 字符)。 Does anyone have any tips?有没有人有任何提示?

    private InputStream downloadCsv(final String countryCode) {
        final String url = baseUrl + countryCode.toUpperCase() + ".zip";
        final String fileName = countryCode.toUpperCase() + ".txt";

        BufferedInputStream in = null;
        ZipInputStream zIn = null;

        try {
            in = new BufferedInputStream(new URL(url).openStream());
            zIn = new ZipInputStream(in, Charset.forName("UTF-8"));
            
            ZipEntry zipEntry;
            
            while ((zipEntry = zIn.getNextEntry()) != null) {
                if (zipEntry.getName().equals(fileName)) {
                    StringBuilder sb = new StringBuilder();
                    
                    int c;
                    while((c = zIn.read()) != -1) {
                        sb.append((char)c);
                        System.out.println((char)c + " : " + c);
                    }

                    return new ByteArrayInputStream(sb.toString().getBytes());
                }
            }
...
more code
...

For the record, I fixed this using @saka1029s advice, using an InputStreamReader , and would mark it as the accepted answer if I could!作为记录,我使用 @saka1029s 建议,使用InputStreamReader解决了这个问题,如果可以的话,我会将其标记为已接受的答案!

I can't promise my code is the cleanest, but it works now:我不能 promise 我的代码是最干净的,但它现在可以工作了:

        BufferedInputStream in = null;
        ZipInputStream zIn = null;
        InputStreamReader zInReader = null;

        try {
            in = new BufferedInputStream(new URL(url).openStream());
            zIn = new ZipInputStream(in);
            
            ZipEntry zipEntry;
            
            while ((zipEntry = zIn.getNextEntry()) != null) {
                if (zipEntry.getName().equals(fileName)) {
                    StringBuilder sb = new StringBuilder();
                    zInReader = new InputStreamReader(zIn);

                    int c;
                    while((c = zInReader.read()) != -1) {
                        sb.append((char)c);
                    }

                    return new ByteArrayInputStream(sb.toString().getBytes());
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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