0

I am building a library for android, and it requires me to unzip files. It works on every single other file except for one file in one particular archive. I get a file not found exception on this. I am not a java expert, or an android expert, but my team is also stumped. Just hoping someone can spot something in my code that could be creating a bug.

public static void unzip(File zipFile,
                         String unzipFilePath,
                         FetchInterface responseHandler,
                         JSONObject json) {

    final int BUFFER_SIZE = 4096;

    String filename;
    InputStream inputStream;
    ZipInputStream zipInputStream;
    ZipEntry zipEntry = null;

    String path = unzipFilePath + File.separator;

    try {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String zipRootDirectory = null;
        inputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
        byte[] buffer = new byte[BUFFER_SIZE];
        int count;
//                Log.d(tag, zipFile.getName());
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {

            filename = zipEntry.getName();
                Log.d(tag, "ZIP ENTRY: " + zipEntry.getName());

            if (zipRootDirectory == null) {
                zipRootDirectory = zipEntry.getName().split("\\/")[0];
//                    Log.d(tag, "ROOT DIR: " + zipRootDirectory);
            }

            if (zipEntry.isDirectory()) {
                Log.d(tag, "ZIPENTRY IS DIR: " + zipEntry.toString());
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
            }

            try {
                FileOutputStream fout = new FileOutputStream(path + filename);

                Log.e(tag, "FILENAME: " + filename);

                while ((count = zipInputStream.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }

                fout.close();

            } catch(FileNotFoundException e) {
                Log.e(tag, "ERROR AT THIS FILE: " + filename);
                e.printStackTrace();
            }
            zipInputStream.closeEntry();
        }

        zipInputStream.close();


        File zipRootDirectoryFile = new File(path + File.separator + zipRootDirectory);
        if (zipRootDirectoryFile.exists()) {
                Log.d(tag, zipRootDirectoryFile.getName());
            File renameFile = new File(path + File.separator + "html" + File.separator);
            zipRootDirectoryFile.renameTo(renameFile);
        } else {
            Log.e(tag, "directory cannot be renamed as it does not exist");
        }


    } catch (IOException e) {
        e.getStackTrace();
    }
}
wdfc
  • 374
  • 4
  • 9
  • Is it a single zip file or a file within the file? The file name and path would also help, as it may be the file's name causing the issue. – Compass Jun 23 '17 at 15:23
  • Its a file within the zip. the paths are all the same, and every other file in the archive unzips properly. The file is called book.html – wdfc Jun 23 '17 at 15:49
  • 1
    OK, that narrows it down. Is the error after `zipInputStream.close();`? If that's the case, you may just be setting the file name and path incorrectly. – Compass Jun 23 '17 at 15:51
  • Yes it is between there(I think) – wdfc Jun 23 '17 at 16:03
  • If you can give me the exact line where it fails it would help. It likely is you're naming your file something weird. – Compass Jun 23 '17 at 16:06
  • java.io.FileNotFoundException: 'path/to/file' But the other files have the same path, just not the same file, and they are writing, it is being caught with FIleNotFoundException – wdfc Jun 23 '17 at 16:12
  • 1
    Looks like this behavior https://stackoverflow.com/questions/9620683/java-fileoutputstream-create-file-if-not-exists - Try to create the file manually to get the root cause. May be permissions, file loc, or something else. – Compass Jun 23 '17 at 16:23
  • It does indeed, I will investigate further, thank you so much – wdfc Jun 23 '17 at 16:25

0 Answers0