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

时间:2020-08-08 09:22:57

标签: java encoding zip

我正在对UTF-8编码的zip文件执行ZipInputStream请求。

我通过OK获得数据,但是特殊的德语字符出了错。

使用此页面(http://kellykjones.tripod.com/webtools/ascii_utf8_table.html),我可以看到我的代码正在从UTF8编码列中打印出两个字符。

即ä是UTF 0xC3,0xA4,我正在得到ä打印出来(它们是0xC3和0xA4字符)。有人有提示吗?

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

1 个答案:

答案 0 :(得分:0)

为记录起见,我使用@ saka1029s的建议并使用 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()); } } 修复了该问题,并在可能的情况下将其标记为可接受的答案!

我不能保证我的代码是最干净的,但是现在可以使用了:

My_struct <typename C::iterator>