阅读一个Zip压缩文件内的文件,并把它的内容到Android中的String它的、并把、压缩文件、文件

2023-09-06 14:09:28 作者:把先生带回家

这是我的第一个问题,虽然我已经使用的堆栈溢出这么多的技巧。但是,对于这种情况我还没有找到一个解决办法。

下面的情况:我有一个压缩文件,我想读一个特定的文件,并把它的内容到字符串变量,将返回并投入Android的一个TextView。我不希望要写入的文件到SD卡,我只是想执行内存操作。

Android安卓手机怎么解压缩rarzipiso压缩文件

例如,里面db.zip我有一个名为packed.txt文件,内容是堆栈权证。在我的方法返回的字符串显示83 116 97 99 107 32 195 161 32(...),这是UTF-8值的字符。我试过到目前为止converting'em一个人readble形式,但没有成功。尝试InputStreamReader的,但我不能作出正确的使用它。我的源 - code是如下。

 进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.util.zip.ZipEntry中;
进口java.util.zip.ZipInputStream中;
进口android.util.Log;

/ **
 *
 * @author乔恩
 * /
公共类DECOM preSS {
  私人字符串_zipFile;
  私人字符串_location;

  公共DECOM preSS(字符串的压缩文件,字符串位置){
    _zipFile = zip文件;
    _location =位置;

    _dirChecker();
  }

  公共字符串解压缩(字符串desiredFile){

      字符串strUnzipped =;

      尝试  {
      的FileInputStream鳍=新的FileInputStream(_zipFile);
      ZipInputStream ZIN =新ZipInputStream(翅);
      ZipEntry的泽= NULL;
      而((泽= zin.getNextEntry())!= NULL){
        Log.v(DECOM preSS的,解压缩+ ze.getName());

        如果(ze.isDirectory()){
          _dirChecker(ze.getName());
        } 其他 {

          // FileOutputStream中FOUT =新的FileOutputStream(_location + ze.getName());

            / ****做以下****更改/
              如果(ze.getName()的toString()。等于(desiredFile)){
                字节[]字节=新字节[(INT)ze.getSize()];
            如果(zin.read(字节,0,bytes.length)== bytes.length){
              strUnzipped =新字符串(字节,UTF-8);
            }
            其他 {
              strUnzipped =读取错误......;
            }

            / *** REMOVED
            如果(ze.getName()== desiredFile){
            对于(INT C = zin.read(); C = -1;!C = zin.read()){
              strUnzipped + = C;
              //fout.write(c);
            } * /
          }

          zin.closeEntry();
          //fout.close();
        }

      }
      zin.close();
    }赶上(例外五){
      Log.e(DECOM preSS,解压,E);
    }

    返回strUnzipped;

  }

  私人无效_dirChecker(字符串DIR){
    文件F =新的文件(_location +方向);

    如果(!f.isDirectory()){
      f.mkdirs();
    }
  }
}
 

解决方案

对不起,这个我最初发帖....正确的跑了过来,您的段落,说明你已经尝试过这一点。

您可以创建一个字符集的InputStreamReader的UTF-8,它会自动将输入数据转换成正确的字符给你。

 ,而((泽= zin.getNextEntry())!= NULL){
    Log.v(DECOM preSS的,解压缩+ ze.getName());
    如果(ze.isDirectory()){
        _dirChecker(ze.getName());
    } 其他 {
        如果(ze.getName()== desiredFile){
            字节[]字节=新字节[ze.getSize()];
            zin.read(字节,0,bytes.length);
            strUnzipped =新字符串(字节,UTF-8);
            / *删除这个,因为它更容易刚才读的所有字节一次
            InputStreamReader的ISR =新的InputStreamReader(ZIN,UTF-8);
            字符C = 0;
            对于(字符C = isr.read(); C = -1;!C = isr.read()){
            strUnzipped + = C;
            }
            * /
        }
        zin.closeEntry();
    }
}
 

请尝试上面的,看看它是如何工作的。

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.