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.
Asked
Active
Viewed 985 times
1 Answers
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
-
when my friend unzip in iOS this zip can create all the folder,sub-folder and file in subfolder. – KuLdip PaTel Apr 28 '17 at 05:43
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