问题描述
这是我的第一个问题,尽管我已经使用了 Stack Overflow 的许多技巧.但是对于这种情况,我还没有找到解决方案.
情况是这样的:我有一个压缩文件,我想读取一个特定文件并将其内容放入一个 String 变量中,该变量将被返回并放入 Android 中的 TextView 中.我不想把文件写入sdcard,我只想进行一次内存操作.
例如,在 db.zip 中,我有一个名为packed.txt 的文件,其内容是"Stack á é ç".我的方法中返回的字符串显示"83 116 97 99 107 32 195 161 32 (...)",它们是字符的 UTF-8 值.到目前为止,我尝试将它们转换为人类可读的形式,但没有成功.尝试了 InputStreamReader 但我无法正确使用它.我的源代码如下.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import android.util.Log; /** * * @author jon */ public class Decompress { private String _zipFile; private String _location; public Decompress(String zipFile, String location) { _zipFile = zipFile; _location = location; _dirChecker(""); } public String unzip(String desiredFile) { String strUnzipped = ""; try { FileInputStream fin = new FileInputStream(_zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { Log.v("Decompress", "Unzipping " + ze.getName()); if(ze.isDirectory()) { _dirChecker(ze.getName()); } else { //FileOutputStream fout = new FileOutputStream(_location + ze.getName()); /**** Changes made below ****/ if (ze.getName().toString().equals(desiredFile)) { byte[] bytes = new byte[(int) ze.getSize()]; if (zin.read(bytes, 0, bytes.length) == bytes.length) { strUnzipped = new String(bytes, "UTF-8"); } else { strUnzipped = "Read error..."; } /*** REMOVED if (ze.getName() == desiredFile) { for (int c = zin.read(); c != -1; c = zin.read()) { strUnzipped += c; //fout.write(c); } */ } zin.closeEntry(); //fout.close(); } } zin.close(); } catch(Exception e) { Log.e("Decompress", "unzip", e); } return strUnzipped; } private void _dirChecker(String dir) { File f = new File(_location + dir); if(!f.isDirectory()) { f.mkdirs(); } } }
推荐答案
抱歉,我最初发布此内容.... 正好在您的段落中指出您已尝试过此内容.
您可以使用"UTF-8"字符集创建 InputStreamReader,它会自动将输入数据转换为正确的字符.
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(); } }
请尝试以上方法,看看效果如何.
其他推荐答案
为什么不将它们加载到字节数组中并使用 new String(byte[]) 或使用 StringBuilder?
StringBuilder sb = new StringBuilder(); for (int c = zin.read(); c != -1; c = zin.read()) { sb.append((byte)c); } ... return sb.toString();
其他推荐答案
... while ((ze = zis.getNextEntry()) != null) { if (ze.getName().equalsIgnoreCase(desiredFile)) { byte[] buffer = new byte[1024]; String texto=""; while (zis.read(buffer) > 0) { texto+=new String(buffer,"ISO-8859-1"); } break; } }
返回文本;
问题描述
this is my first question, although I've already used so many tips from Stack Overflow. But for this situation I haven't found a solution yet.
Here's the situation: I have a zipped file and I want to read a specific file and put its content into a String variable, that will be returned and put into a TextView in Android. I don't want the file to be written to sdcard, I just want to perform a memory operation.
For example, inside db.zip I have a file named packed.txt, which content is "Stack á é ç". The returned String in my method shows "83 116 97 99 107 32 195 161 32 (...)" which are the UTF-8 values for the characters. I tried so far converting'em to a human-readble form, but no success. Tried InputStreamReader but I couldn't make a correct use of it. My source-code is below.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import android.util.Log; /** * * @author jon */ public class Decompress { private String _zipFile; private String _location; public Decompress(String zipFile, String location) { _zipFile = zipFile; _location = location; _dirChecker(""); } public String unzip(String desiredFile) { String strUnzipped = ""; try { FileInputStream fin = new FileInputStream(_zipFile); ZipInputStream zin = new ZipInputStream(fin); ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { Log.v("Decompress", "Unzipping " + ze.getName()); if(ze.isDirectory()) { _dirChecker(ze.getName()); } else { //FileOutputStream fout = new FileOutputStream(_location + ze.getName()); /**** Changes made below ****/ if (ze.getName().toString().equals(desiredFile)) { byte[] bytes = new byte[(int) ze.getSize()]; if (zin.read(bytes, 0, bytes.length) == bytes.length) { strUnzipped = new String(bytes, "UTF-8"); } else { strUnzipped = "Read error..."; } /*** REMOVED if (ze.getName() == desiredFile) { for (int c = zin.read(); c != -1; c = zin.read()) { strUnzipped += c; //fout.write(c); } */ } zin.closeEntry(); //fout.close(); } } zin.close(); } catch(Exception e) { Log.e("Decompress", "unzip", e); } return strUnzipped; } private void _dirChecker(String dir) { File f = new File(_location + dir); if(!f.isDirectory()) { f.mkdirs(); } } }
推荐答案
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();
其他推荐答案
... while ((ze = zis.getNextEntry()) != null) { if (ze.getName().equalsIgnoreCase(desiredFile)) { byte[] buffer = new byte[1024]; String texto=""; while (zis.read(buffer) > 0) { texto+=new String(buffer,"ISO-8859-1"); } break; } }
return texto;