mxride
mxride

Reputation: 21

Android UNZIP Inputstream problem

I am trying to unzip a file located on SDCARD of Android device. If the ZIP file only contains files NOT FOLDERS everything is fine. However the production file that I want the app to unzip contains multiple directories and sub-directories. This is where I run into issues.

The code isnt creating the folder structure required. If I manually create the folders prior to running the class it unzips as planned, but in the future I wont know the directory structure and need the code to create the proper folder structure. Here is the code I'm working with

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class HomeActivity extends Activity
{
    private TextView tv;

    private static final String ROOT_FOLDER = Environment.getExternalStorageDirectory()
            + File.separator + "FacebookPhotos";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        tv.append("Reading zip file from assets folder...");

        // Start the unzipping process
        thread.start();
    }

    private Thread thread = new Thread()
    {

        @Override

        public void run()
        {
            sendMessage("-----------------------------------------------");

            // Create a directory in the SDCard to store the files
            File file = new File(ROOT_FOLDER);
            if (!file.exists())
            {
                file.mkdirs();
            }
            else
            {
                file.delete();
            }
            try
            {
                // Open the ZipInputStream


                ZipInputStream inputStream = new ZipInputStream(new FileInputStream("/sdcard/Photos/photos.zip"));


                // Loop through all the files and folders
                for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream
                        .getNextEntry())
                {
                    sendMessage("Extracting: " + entry.getName() + "...");

                   String innerFileName = ROOT_FOLDER + File.separator + entry.getName();

                    File innerFile = new File(innerFileName);
                    if (innerFile.exists())
                    {
                        innerFile.delete();
                    }

                    // Check if it is a folder
                    if (entry.isDirectory())


                    {
                     // Its a folder, create that folder
                        innerFile.mkdirs();

                    }
                    else
                    {
                        // Create a file output stream
                        FileOutputStream outputStream = new FileOutputStream(innerFileName);
                        final int BUFFER = 2048;

                        // Buffer the output to the file
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream,
                                BUFFER);

                        // Write the contents
                        int count = 0;
                        byte[] data = new byte[BUFFER];
                        while ((count = inputStream.read(data, 0, BUFFER)) != -1)
                        {
                            bufferedOutputStream.write(data, 0, count);
                        }

                        // Flush and close the buffers
                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();
                    }
                    sendMessage("DONE");

                    // Close the current entry
                    inputStream.closeEntry();
                }
                inputStream.close();
                sendMessage("-----------------------------------------------");
                sendMessage("Unzipping complete");

            }
            catch (IOException e)
            {
                sendMessage("Exception occured: " + e.getMessage());
                e.printStackTrace();
            }
        }

    };

    private Handler handler = new Handler()
    {

        @Override
        public void handleMessage(Message msg)
        {
            tv.append("\n" + msg.getData().getString("data"));
            super.handleMessage(msg);
        }

    };

    private void sendMessage(String text)
    {
        Message message = new Message();
        Bundle data = new Bundle();
        data.putString("data", text);
        message.setData(data);
        handler.sendMessage(message);
    }
}

Upvotes: 2

Views: 3741

Answers (1)

Bill Brasky
Bill Brasky

Reputation: 2644

Check out this answer

Android - Unzip a folder?

I think sometimes in ZIP files, the directory entries may be added after their files. In this case, you won't get the directory before the file. The linked example calls file.getParentFile().exists() for regular files and creates that directory if it doesn't eixst.

Upvotes: 3

Related Questions