The ultimate java decompression package (solving Chinese garbled characters)

    The file operation is not difficult, but it is very commonly used in projects. Here is a final code for decompressing the compressed package: the zip format needs to use the java.util.zip package, and the rar format needs to use com.github.junrar.

    Without further ado, the code is as follows:

 

 

/**
      * Unzip the compressed file in zip format to the specified location
      *
      * @param zipFileName
      * Full path to compressed file
      * @param dstDirectoryPath
      * Unzip the directory
      * @throws Exception
      */
     public static boolean unZipFile(String zipFileName, String dstDirectoryPath)
                throws Exception {
           // The problem of garbled characters and the problem of duplication of file content
           System.setProperty("sun.zip.encoding",
                     System.getProperty("sun.jnu.encoding"));
           try {
                File dstDiretory = new File(dstDirectoryPath);
                if (!dstDiretory.exists()) {// When the target directory does not exist, create the folder
                     dstDiretory.mkdirs();
                }
                File f = new File(zipFileName);
                if ((!f.exists()) && (f.length() <= 0)) {
                     throw new Exception("The file to be decompressed does not exist!");
                }
               ZipFile zipFile = new ZipFile(zipFileName, Charset.forName("gbk")); // handle the problem of garbled Chinese file names
                String strPath, gbkPath, strtemp;
                File tempFile = new File(dstDirectoryPath);
                strPath = tempFile.getAbsolutePath();
                Enumeration<?> e = zipFile.entries();
                while (e.hasMoreElements()) {
                     ZipEntry zipEnt = (ZipEntry) e.nextElement();
                     gbkPath = zipEnt.getName();
                     // Determine whether it is a directory or a file. If it is a file, it is directly parsed and uploaded
                     // If it is a directory, create a folder recursively to process files
                     if (zipEnt.isDirectory()) {
                           strtemp = strPath + File.separator + gbkPath;
                           File dir = new File(strtemp);
                           dir.mkdirs();
                           continue;
                     } else {
                           // read and write files
                           InputStream is = zipFile.getInputStream(zipEnt);
                           BufferedInputStream bis = new BufferedInputStream(is);
                           gbkPath = zipEnt.getName();
                           strtemp = strPath + File.separator + gbkPath;

                           // Create a directory to deal with directly for fear of duplicate name coverage
                           String strsubdir = gbkPath;
                           for (int i = 0; i < strsubdir.length(); i++) {
                                if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
                                     String temp = strPath + File.separator
                                                + strsubdir.substring(0, i);
                                     File subdir = new File(temp);
                                     if (!subdir.exists())
                                           subdir.mkdir();
                                }
                           }
                           FileOutputStream fos = new FileOutputStream(strtemp);
                           BufferedOutputStream bos = new BufferedOutputStream(fos);
                           int c;
                           while ((c = bis.read()) != -1) {
                                bos.write((byte) c);
                           }
                           bos.close();
                           fos.close();

                     }
                }

                return true;
           } catch (Exception e) {
                e.printStackTrace ();
                return false;
           }
     }

     /**
      * According to the original rar path, extract it to the specified folder.
      *
      * @param srcRarPath
      * original rar path
      * @param dstDirectoryPath
      * unzipped folder
      */
     public static boolean unRarFile(String srcRarPath, String dstDirectoryPath) {
           File dstDiretory = new File(dstDirectoryPath);
           if (!dstDiretory.exists()) {// When the target directory does not exist, create the folder
                dstDiretory.mkdirs();
           }
           Archive a = null;
           File rarFile = null;
           try {
                rarFile = new File(srcRarPath);
                if (!rarFile.exists()) {
                     return false;
                } else {
                     a = new Archive(rarFile);
                     if (a != null) {
                           // a.getMainHeader().print(); // Print file information.
                           FileHeader fh = a.nextFileHeader();
                           while (fh != null) {
                                // To prevent the problem of Chinese garbled characters in the file name

                                String fileName = fh.getFileNameW().isEmpty() ? fh
                                           .getFileNameString() : fh.getFileNameW();
                                if (fh.isDirectory()) { // folder
                                     File fol = new File(dstDirectoryPath
                                                + File.separator + fileName);
                                     fol.mkdirs();
                                } else { // file
                                     File out = new File(dstDirectoryPath
                                                + File.separator + fileName.trim());
                                     try {
                                           if (!out.exists()) {
                                                if (!out.getParentFile().exists()) {// The relative path may be multi-level, and a parent directory may need to be created.
                                                     out.getParentFile().mkdirs();
                                                }
                                                out.createNewFile();
                                           }
                                           FileOutputStream os = new FileOutputStream(out);
                                           a.extractFile(fh, os);
                                           os.close();
                                     } catch (Exception ex) {
                                           ex.printStackTrace();
                                     }
                                }
                                fh = a.nextFileHeader();
                           }
                           a.close();
                     }
                }
                return true;
           } catch (Exception e) {
                e.printStackTrace ();
           }

           return false;
     }


/**
      * Delete file directories and files
      *
      * @param delFile
      */
     private void deleteFile(File delFile) {

           // delete a single file
           if (delFile.exists()) {
                if (delFile.isFile()) {
                     delFile.delete();
                } else if (delFile.isDirectory()) {
                     // If the directory traverses the files in the directory, delete the file and delete the directory
                     File[] files = delFile.listFiles();
                     for (int i = 0; i < files.length; i++) {
                           deleteFile(files[i]);
                     }
                     delFile.delete();// delete the directory
                }
           }
     }

     /**
      * Delete local directories and files
      *
      * @param sPath
      * absolute path
      */
     private void deleteFolder(String sPath) {

           File file = new File(sPath);
           // Check if the directory or file exists
           if (!file.exists()) { // does not exist return false
                return;
           } else {
                // Determine if it is a file
                deleteFile(file);
           }

     }
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403116&siteId=291194637