ZipException while decompressing .zip programmatically - android

I am trying to decompressing .zip, programmatically in my application with the help of this code,
public void unzip(String _zipFile, String _targetLocation) {
dirChecker(_targetLocation);
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
Log.i("xoxo", "unzip: completed!!!");
} catch (Exception e) {
Log.i("xoxo", "unzip: err: - " + e.toString());
}
}
Everything is fine, but the problem is I am getting an error,
java.util.zip.ZipException: only DEFLATED entries can have EXT descriptor
While trying to decompress .zip created by server that is running on linux environment.

You can try this library
https://github.com/ankitdubey021/ExtractionLib
void extract(){
try {
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "value1.jar");
Extract.extract(outputFile, new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "extration").toString());
Toast.makeText(MainActivity.this, "Extracted!", Toast.LENGTH_SHORT).show();
}catch(Exception e){
System.out.println(e);
}}
It will easily extract your zip file. 3 years back I used it in a project. and still working.

Related

Fixed a Zip Path Traversal Vulnerability in Android app ,but Google Play still issued a warning

I have some apps on google play, and they are all prompted that there is an unsafe unzip mode. After I fixed this problem with the same code, most of my APPs no longer have warnings, but one APP still has warnings. I don't know what the problem is.
public static boolean unZipFile(String unZipPath, String zipPath) {
unZipPath = createSeparator(unZipPath);
BufferedOutputStream bos = null;
ZipInputStream zis = null;
boolean result = false;
try {
String filename;
zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipPath)));
ZipEntry ze;
byte[] buffer = new byte[BUFF_SIZE];
int count;
while ((ze = zis.getNextEntry()) != null) {
filename = ze.getName();
//zip fix
File zeFile = new File(unZipPath, filename);
String zePath = zeFile.getCanonicalPath();
File targetFile = new File(unZipPath);
String targetPath = targetFile.getCanonicalPath();
if (!TextUtilsEx.startsWith(zePath, targetPath + File.separator)) {
throw new ZipException("Illegal name: " + filename);
}
Logger.d(TAG, "ze.getName() = " + filename);
createSubFolders(filename, unZipPath);
if (ze.isDirectory()) {
File fmd = new File(unZipPath + filename);
fmd.mkdirs();
continue;
}
Logger.d(TAG, "unzip file = " + unZipPath + filename);
bos = new BufferedOutputStream(new FileOutputStream(unZipPath + filename));
while ((count = zis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
}
result = true;
} catch (Throwable e) {
Logger.e(TAG, e);
} finally {
try {
if (zis != null) {
zis.closeEntry();
zis.close();
}
if (bos != null) {
bos.close();
}
} catch (Throwable e) {
Logger.e(TAG, e);
}
}
return result;
}
I try to use the modified scheme in this document.
https://support.google.com/faqs/answer/9294009
I used the exception zip file to test, and I can indeed verify that the fix works.

Getting backward slash while unzip folder containing files like: /sdcard/folder/subfolder\file.xml in android

My Zipped folder contains sub-folder with files but while extracting it, I am not able to achieve the same hierarchy. I'm getting the unzipped structure as follows:-
/storage/emulated/0/unzipped_folder/sub_folder\main.png
/storage/emulated/0/unzipped_folder/sub_folder\test.xml
So while extracting it, I'm not able to get sub_folder as a directory.
I'm using below code while extracting the zip file.
public static void unzip(String zipFile, String location) throws IOException {
try {
File f = new File(location);
if (!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = location + File.separator + ze.getName();
if (ze.isDirectory()) {
File unzipFile = new File(path);
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
FileOutputStream fout = new FileOutputStream(path, false);
try {
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
} finally {
fout.close();
}
}
}
} finally {
zin.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ZIP STU", "Unzip exception", e);
}
}
Please help, I'm stuck in this for more than 2 days.
Thanks!
Finally I'm able to solve this issue using below code.
public static void unzipEPub(File zipFile, File destinationDir){
ZipFile zip = null;
try {
int DEFUALT_BUFFER = 1024;
destinationDir.mkdirs();
zip = new ZipFile(zipFile);
Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
while (zipFileEntries.hasMoreElements()) {
ZipEntry entry = zipFileEntries.nextElement();
String entryName = entry.getName();
entryName = entryName.replace("\\","/");
File destFile = new File(destinationDir, entryName);
File destinationParent = destFile.getParentFile();
if (destinationParent != null && !destinationParent.exists()) {
destinationParent.mkdirs();
}
if (!entry.isDirectory()) {
BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
int currentByte;
byte data[] = new byte[DEFUALT_BUFFER];
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER);
while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) > 0) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zip != null) {
try {
zip.close();
} catch (IOException ignored) {
}
}
}
}

Android unzip zip file from internal storage

I am downloading zip file from server to internal storage. And after downloading I want to unzip that file in internal storage itself. My file is downloaded to internal storage and I can see that but while unzip I am not able to read it. Below is my unzipfile() method. Can anyone tell where I am going wrong?
public void unzipfile()
{
try {
Log.d("unzipiing","unzipping");
ContextWrapper cw = new ContextWrapper(context);
String name_="foldername"; //Folder name in device android/data/
File directory = cw.getDir(name_, Context.MODE_PRIVATE);
File mypath=new File(directory,"dwnld");
FileOutputStream fout = new FileOutputStream(mypath);
File yourFile = new File(directory,"dwnld.zip");
Log.d("unzipiing","filepath -" + yourFile.getPath());
FileInputStream fin = new FileInputStream(yourFile.getPath());
ZipInputStream zin = new ZipInputStream(fin);
Log.d("unzipiing","zin size -" + zin.available());
// zin.available() give -1 in console log
BufferedInputStream in = new BufferedInputStream(zin);
BufferedOutputStream out = new BufferedOutputStream(fout);
byte b[] = new byte[zin.available()];
int n;
Log.d("unzip","n - " + in.read(b,0,1024));
while ((n = in.read(b,0,1024)) >= 0) {
out.write(b,0,n);
Log.d("unzip byte"," - " + n);
}
out.flush();
out.close();
in.close();
fin.close();
zin.close();
}
catch (Exception e){
}
}
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
use this method for unzip
public void unzip(String _zipFile, String _targetLocation) {
//create target location folder if not exist
dirChecker(_targetLocatioan);
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
//create dir if required while unzipping
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch (Exception e) {
System.out.println(e);
}
}
check the path
public void dirChecker(String filepath)
{
File file = new File(filePath);
if(file.exists())
//Do something
else
// Do something else.
}

Android unzip database file from assets folder

I am building the app which i want to unzip the database file from assets. But i am getting this errorjava.io.FileNotFoundException: /file:/android_asset/kjv.zip: open failed: ENOENT (No such file or directory).
I am using this code for unzip the file,
public void unzip(String _zipFile, String _targetLocation) {
//create target location folder if not exist
dirChecker(_targetLocation);
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
//create dir if required while unzipping
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_targetLocation + 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("error", e + "");
}
}
private void dirChecker(String dir) {
File f = new File(dir);
if (!f.isDirectory()) {
f.mkdirs();
}
}
And call using this,
Uri path = Uri.parse("file:///android_asset/kjv.zip");
inputPath = path.toString();
ZipManager zipManager = new ZipManager();
zipManager.unzip(inputPath, outputPath);
I tried, but it was error every time. Can't understand what is the problem. If you have alternative solution then please suggest me. Help me. Thank you.

Android external write permitted but file not found exception

In the emulator I'm trying to write to the file:
/mnt/sdcard/Android/data/com.Me.MyApp/files/myFile.txt
I have set external write permissions in the Manifest, but I keep receiving a file not found exception. The emulator is configured to have an sd-card.
Why might that be?
InputStream is = c.getResources().openRawResource(R.raw.my_raw_file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
try
{
while (zis.getNextEntry() != null)
{
File destFile = new File(destinationPath);
// EXCEPTION THROWN NEXT LINE!
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[BUF_SIZE];
int count;
while ((count = zis.read(buffer)) != -1)
{
os.write(buffer, 0, count);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
zis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
Try below code
String path = "/mnt/sdcard/Android/data/com.Me.MyApp/files";
File mFile = new File(path);
mFile.mkdirs();
Nammari's answer plus:
Android Tutorial: Creating and Using an SD Card in the Emulator
http://www.streamhead.com/android-tutorial-sd-card/

Categories

Resources