Search code examples
javaandroideclipseunzip

getNextEntry() doesn't display folder as an entry?


Hi I'm new to android programming.

I'm trying to create a program to unzip a zipped file in my sd card and I noticed something when I debug.

public void testZipOrder() throws Exception {
            File file = new File(_zipFile);
            zis = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry = null;
            while ( (entry = zis.getNextEntry()) != null ) {
             System.out.println( entry.getName());
            }
        }
    } 

this give me an output of :

06-27 00:42:06.360: I/System.out(15402): weee.txt
06-27 00:42:06.360: I/System.out(15402): hi/bye.txt
06-27 00:42:06.360: I/System.out(15402): hi/hiwayne.txt

isn't it suppose to give

weee.txt
hi/
hi/bye.txt
hi/hiwayne.txt

or something that displays its folder instead?


Solution

  • I tried this on my own environment using a test zip file created with 7zip and the following method:

    public void testZipOrder() throws Exception {
        File file = new File("zip.zip");
        ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
        ZipEntry entry = null;
        while ( (entry = zis.getNextEntry()) != null ) {
         System.out.println( entry.getName());
        }
        zis.close();
    }
    

    Note this method is effectively identical to yours.

    The resulting output was:

    file1.txt
    folder1/
    folder1/file2.txt
    folder1/folder2/
    folder1/folder2/file3.txt
    

    Which is, I believe, what you are looking for. As such I expect the problem is with the zip file itself, not your code. It is likely that your zip file does not contain an entry for the directory "hi/".

    See here for a basic description of how zip files are structured.