0

When i was unzip file.it's make file name like Movies\Hollywood\spider-Man. Actually Movies is a Folder,Hollywood is a Folder in Movies and spider-Man is file in Hollywood.

A.B.
  • 1,554
  • 1
  • 14
  • 21
KuLdip PaTel
  • 1,069
  • 7
  • 19
  • 1
    Please paste the code – Rahul Giradkar Apr 28 '17 at 04:57
  • public static void unzip(String Filepath,String DestinationFolderPath){ try { ZipFile zipFile = new ZipFile(Filepath); zipFile.extractAll(DestinationFolderPath); } catch (ZipException e) { e.printStackTrace(); } } – KuLdip PaTel Apr 28 '17 at 05:07
  • provide some code, it would be hard to tell without looking into code. Pleaes make sure that the specified entry was file (and NOT directory) when zip was created. – A.B. Apr 28 '17 at 05:33
  • i'm using zip4j library for unzip. i get all file but like as that a write in description of question. – KuLdip PaTel Apr 28 '17 at 05:38
  • see http://stackoverflow.com/a/40125872/6478047 answer – Manohar Apr 28 '17 at 05:45

1 Answers1

3

If Movies\Hollywood\spider-Man is a file while creating the zip it should be extracted as file, no matters whether it has extension or not (like *.mp4, *.flv)

You can rely on java APIs under namespace java.util.zip, the documentation link is here

Written some code which extracts only zip files, it should extract the file entry as file (no gzip, rar is supported).

private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException
{
    int BUFFER = 8192;
    File file = zipFile;
    //This can throw ZipException if file is not valid zip archive
    ZipFile zip = new ZipFile(file);
    String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName());
    //Create destination directory
    new File(newPath).mkdir();
    Enumeration zipFileEntries = zip.entries();

    //Iterate overall zip file entries
    while (zipFileEntries.hasMoreElements())
    {
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(newPath, currentEntry);
        File destinationParent = destFile.getParentFile();
        //If entry is directory create sub directory on file system
        destinationParent.mkdirs();

        if (!entry.isDirectory())
        {
            //Copy over data into destination file
            BufferedInputStream is = new BufferedInputStream(zip
            .getInputStream(entry));
            int currentByte;
            byte data[] = new byte[BUFFER];
            //orthodox way of copying file data using streams
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    return true;//some error codes etc.
}

The routine does not perform any exception handling, please catch the ZipException and IOException inside driver code.

A.B.
  • 1,554
  • 1
  • 14
  • 21