I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.
Please feel free to use the form below to contact me.
I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.
Please feel free to use the form below to contact me.
I wrote an article a few months ago, which walked you through the process of zipping files programmatically with Java - in an Android environment. Due to popular demand, I've decided to write up another article on how to unzip files.
The process is pretty similar, and you could probably combine them both in a single class, but for this example, I made a class called Decompress.
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
*
* @author jon
*/
public class Decompress {
private String _zipFile;
private String _location;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
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());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
Well, that's the class - tested on my Nexus One and works!
Here's an example of how to use it:
String zipFile = Environment.getExternalStorageDirectory() + "/files.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
That should unzip files.zip and put the contents into /sdcard/unzipped/, as was specified.
Check out my other article, if you want to find out how to zip files - Zipping Files with Android (Programmatically)
http://stackoverflow.com/questions/3975847/extrakting-zip-to-sd-card-is-very-slow-how-can-i-optimize-performance/10312761#10312761
just wanted to tell you how happy I'm that I found your tutorial about unzipping files. It helped me alot! Thanks and have a great day. With your help it got clear how to tackle the problem!
??
Other then that it works great thanks
I have a foler on SD Card containing a ZIP archive,
Have created a test project with the above Class as MAIN, However I cant seem to figure out where to implement
String zipFile = Environment.getExternalStorageDirectory() + "/files.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip()
Any help is much appreciated..
For ex:if files.zip contains[
folder_image -> a.png & b.png
folder_css ->x.css
file.txt ]
Iam getting Err like:" File Not found exception .../a.png " what is the problem?
I have a problem. When I unzip a file and if it contains any folder which has white spaces in its name then unziping stops there and does not go ahead.How to deal if we have any folder having white space in it?
Please help.
Thanks
Parvendra
Regarding the slow performance in the emulator, I've found that it goes much faster (a few seconds as opposed to a minute in the emulator) if you copy the bytes into a buffer instead of individually:
byte[] buffer = new byte[1024];
int length;
...
// replace for loop with:
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
Good Tutorial
I tested a 10mb file on mine and it went quick.
for (int c = zin.read(); c != -1; c = zin.read()){
Log.v("Decompress", "For Looping "+ ze.getName()); fout.write(c);
}
Might be worth logging the process, to see where it's slowing down (Log.v("", "")).