Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4019260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:04:59+00:00 2026-05-20T10:04:59+00:00

I have a nice java code that unzips a .zip file. But problem with

  • 0

I have a nice java code that unzips a .zip file. But problem with this code is

  1. i need to create target folders(Note:Only folders not file) before running this code.
  2. Otherwise i will get path not found exception.

So This code wont work if zip file content is not known before. so i think this is useless code. Anyone have better logic? or Bellow code need to be edited?

package com.mireader;
import android.os.Environment;
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; 
            byte[] buffer = new byte[1024];
            int length;
            int i=0;  
            while ((ze = zin.getNextEntry()) != null) { 
                Log.v("t", ze.toString());
                Log.v("Decompress", "Unzipping " + ze.getName()); 

                if(ze.isDirectory()) {
                    Log.i("my","Comes to if");
                    _dirChecker(ze.getName()); 
                }
                else { 
                    Log.i("my","Comes to else");
                    FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
                    while ((length = zin.read(buffer))>0) {
                        fout.write(buffer, 0, length);
                    }
                    zin.closeEntry(); 
                    fout.close(); 
                } 
            }
            zin.close(); 
            Log.i("My tag","Success");
        }catch(Exception e) { 
            Log.e("Decompress", "unzip", e); 
        }     

    } 

    private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 
    if(!f.isDirectory()) { 
        Log.i("mytag", "Creating new folder");
        f.mkdirs(); 
        System.out.print("stp:"+f.getName());
    } 
  } 
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:04:59+00:00Added an answer on May 20, 2026 at 10:04 am

    You can avoid the following piece of code

    if(ze.isDirectory()) {
      Log.i("my","Comes to if");
      _dirChecker(ze.getName()); 
    }
    

    and add code similar to the one below in the file creator else part. It worked for me by creating the entire parent folders.

    File file = createFile((baseDirectory +"/" + zipFile.getName()));
    file.getParentFile().mkdirs();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a need to generate Java source code. We do this by modeling
In JPA the Entities are nice annotated Plain Old Java Objects. But I have
I could do this myself given time but does anyone have a nice asp.net
This discussion started over here but I thought it would be nice to have
I'd like to have a nice, tidy way of expressing the following java code
I often have to debug Java code which was written so that there is
I have some image processing Java code in Android that acts upon two large
An enum in Java implements the Comparable interface. It would have been nice to
I have a nice and lovely Django site up and running, but have noticed
I would like to have a nice template for doing this in development. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.