Read a file inside a Zip archive and put its content into a String in Android

核能气质少年 提交于 2019-11-30 10:34:50

Sorry for my initial posting of this .... ran right over your paragraph stating you had tried this.

You can create the InputStreamReader with a charset of "UTF-8" which will automatically convert the input data into the correct characters for you.

while ((ze = zin.getNextEntry()) != null) { 
    Log.v("Decompress", "Unzipping " + ze.getName()); 
    if(ze.isDirectory()) { 
        _dirChecker(ze.getName()); 
    } else {
        if (ze.getName() == desiredFile) {
            byte[] bytes= new byte[ze.getSize()];
            zin.read(bytes, 0, bytes.length);
            strUnzipped= new String( bytes, "UTF-8" );
            /* Removing this as it is easier to just read all the bytes in at once
            InputStreamReader isr= new InputStreamReader(zin, "UTF-8");
            char c= 0;
            for (char c = isr.read(); c != -1; c = isr.read()) {
            strUnzipped += c;
            } 
            */
        }
        zin.closeEntry(); 
    }
} 

Please try the above and see how it works out.

Why not load them into a byte array and use new String(byte[]) or use the StringBuilder?

StringBuilder sb = new StringBuilder();
for (int c = zin.read(); c != -1; c = zin.read()) {
    sb.append((byte)c);
}
...
return sb.toString();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!