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();
}
}
}