I have below code for unzip file. (from a thread in stackoverflow)
/**
* Unzip a zip file. Will overwrite existing files.
*
* @param zipFile Full path of the zip file you'd like to unzip.
* @param location Full path of the directory you'd like to unzip to (will be created if it doesn't exist).
* @throws IOException
*/
public static void unzip(String zipFile, String location, String excludePath) throws IOException {
int size;
byte[] buffer = new byte[BUFFER_SIZE];
try {
if ( !location.endsWith("/") ) {
location += "/";
}
File f = new File(location);
if(!f.isDirectory()) {
f.mkdirs();
}
FileInputStream fin = new FileInputStream(zipFile);
BufferedInputStream bin = new BufferedInputStream(fin, BUFFER_SIZE);
ZipInputStream zin = new ZipInputStream(bin);
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
//String path = location + ze.getName();
String unzipFilePath = ze.getName().replace(excludePath, "");
String path = location + unzipFilePath;
File unzipFile = new File(path);
if (ze.isDirectory()) {
if(!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
// check for and create parent directories if they don't exist
File parentDir = unzipFile.getParentFile();
if ( null != parentDir ) {
if ( !parentDir.isDirectory() ) {
parentDir.mkdirs();
}
}
// unzip the file
FileOutputStream out = new FileOutputStream(unzipFile, false);
BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
try {
while ( (size = zin.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
fout.write(buffer, 0, size);
}
zin.closeEntry();
}
finally {
fout.flush();
fout.close();
out.close();
}
}
}
}
finally {
zin.close();
fin.close();
bin.close();
}
}
catch (Exception e) {
Log.e("bug", "Unzip exception", e);
}
buffer = null;
System.gc();
}
I have no problem in unzipping file. But as my program continue to run. It tried to show the unzipped jpeg image ( 1000px x 800px) by below code. I created a button to show one image at a time.
fisImg = new FileInputStream(new File(imgPath[i]));
Bitmap imgBitmap = BitmapFactory.decodeStream(fisImg );
It has no problem in loading the first image, but when i pressed next button, it tried to load the next image, it called out of memory exception. I wonder if my unzip code has memory leakage?