Monday, January 9, 2017

How to extract zip folder which is having files and sub-directories ?


How to extract zip folder which is having files and sub-directories ?

---------------------------------------------------------------------------------------------------------------
// source of zip folder


String zipFilePath = “D:/apache-tomcat-8.0.39/webapps/examples/test.zip”

//Destination folder, where to extract files and folders
String destDir=” C:/Users/F1038/AppData/Roaming/YourFolder”

Call below method from your main method:
-------------------------------------------------------------------------------------

unzip(zipFilePath, destDir);

//method to check folder tree and create folder or files while extracting it to destination path

private static void unzip(String zipFilePath, String destDir) {
              if (zipFilePath.contains(".zip")) {
                     try {
                           File dir = new File(destDir);
                           // create output folder/directory if it doesn't exist
                           if (!dir.exists())
                                  dir.mkdirs();
                           // Opening the zip file
                           ZipFile zipFile = new ZipFile(zipFilePath);
                           Enumeration<?> enu = zipFile.entries();
                           while (enu.hasMoreElements()) {
                                  ZipEntry zipEntry = (ZipEntry) enu.nextElement();

                                  String fileName = zipEntry.getName();

                                 
                                  File file = new File(destDir + File.separator + fileName);
                                  System.out.println("Unzipping to : " + file.getAbsolutePath());
                                  if (fileName.endsWith("/")) {
                                         file.mkdirs();
                                         continue;
                                  }

                                  File parent = file.getParentFile();
                                  if (parent != null) {
                                         parent.mkdirs();
                                  }

                                  // Extracting file here
                                  InputStream is = zipFile.getInputStream(zipEntry);
                                  FileOutputStream fos = new FileOutputStream(file);
                                  byte[] bytes = new byte[1024];
                                  int length;
                                  while ((length = is.read(bytes)) >= 0) {
                                         fos.write(bytes, 0, length);
                                  }
                                  is.close();
                                  fos.close();

                           }
                           zipFile.close();
                     } catch (IOException e) {
                           e.printStackTrace();
                     }
              }
       }



No comments:

Post a Comment

Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...