Reputation: 539
I have an UnZip class to get images from a database
class UnZip extends AsyncTask<String, Integer, String> {
private String _mArchivePath;
private String _mOutPutStream;
private int per;
public UnZip(String mArchivePath,String mOutPutStream) {
_mArchivePath = mArchivePath;
_mOutPutStream = mOutPutStream;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream inputstream;
ZipInputStream zipinputstream;
try {
String filename;
inputstream = new FileInputStream(_mArchivePath);
zipinputstream = new ZipInputStream(new BufferedInputStream(inputstream));
ZipEntry mZipEntry;
byte[] buffer = new byte[32*1024];
int count;
while ((mZipEntry = zipinputstream.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + mZipEntry.getName());
filename = mZipEntry.getName();
per++;
publishProgress(per);
if (mZipEntry.isDirectory()) {
File fmd = new File(_mOutPutStream + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fileoutputstream = new FileOutputStream(_mOutPutStream + filename);
while ((count = zipinputstream.read(buffer)) != -1) {
fileoutputstream.write(buffer, 0, count);
}
fileoutputstream.close();
zipinputstream.closeEntry();
}
zipinputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
It works great with a database.zip containing images(32MB~) but when I tried it with the same database.zip but with some images extra(43MB~) it gives me this error:
03-03 23:42:00.200: V/Decompress(11593): Unzipping /database/weed/ak_47_1.jpg
03-03 23:42:00.202: W/System.err(11593): java.io.FileNotFoundException: /storage/emulated/0/unzipped/database/weed/ak_47_1.jpg: open failed: ENOENT (No such file or directory)
03-03 23:42:00.204: W/InputMethodManagerService(584): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@cce61f3 attribute=android.view.inputmethod.EditorInfo@aa9eeb0, token = android.os.BinderProxy@2db5667
I found the problem, it comes when I try to unzip a file with subfolders, if the subfolders don't exist the app gives me this error. Momentarily I changed the main code, so when the app starts it automatically creates the folders, because I know the structure of the zip, but if I want to use another zip what can I do?
Upvotes: 2
Views: 682
Reputation: 1756
It looks like there's something wrong with the code you use to unpack the ZIP, i.e. the creation of subfolders doesn't work as expected.
The best thing to do is to investigate that, as @BrentM suggested, however if you're in a rush, a quick search on SO could be of help:
How to unzip files programmatically in Android?
I used the following method from that thread and can confirm it works perfectly well:
private boolean unpackZip(String path, String zipname)
{
InputStream is;
ZipInputStream zis;
try
{
String filename;
is = new FileInputStream(path + zipname);
zis = new ZipInputStream(new BufferedInputStream(is));
ZipEntry ze;
byte[] buffer = new byte[1024];
int count;
while ((ze = zis.getNextEntry()) != null)
{
filename = ze.getName();
// Need to create directories if not exists, or
// it will generate an Exception...
if (ze.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
FileOutputStream fout = new FileOutputStream(path + filename);
while ((count = zis.read(buffer)) != -1)
{
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
zis.close();
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
return true;
}
Upvotes: 1