/**
 * Copyright (c) 2015-2020, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.utils;

import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class FileUtil {

    public static String getSuffix(String fileName) {
        if (fileName != null && fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf("."));
        }
        return null;
    }


    public static String removePrefix(String src, String prefix) {
        if (src != null && src.startsWith(prefix)) {
            return src.substring(prefix.length());
        }
        return src;
    }


    public static String removeRootPath(String src) {
        return removePrefix(src, PathKit.getWebRootPath());
    }

    public static String readString(File file) {
        ByteArrayOutputStream baos = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            for (int len = 0; (len = fis.read(buffer)) > 0; ) {
                baos.write(buffer, 0, len);
            }
            return new String(baos.toByteArray(), JFinal.me().getConstants().getEncoding());
        } catch (Exception e) {
        } finally {
            close(fis, baos);
        }
        return null;
    }

    public static void writeString(File file, String string) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file, false);
            fos.write(string.getBytes(JFinal.me().getConstants().getEncoding()));
        } catch (Exception e) {
        } finally {
            close(null, fos);
        }
    }

    private static void close(InputStream is, OutputStream os) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
    }

    public static void unzip(String zipFilePath) throws IOException {
        String targetPath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));
        unzip(zipFilePath, targetPath, true);
    }

    public static void unzip(String zipFilePath, String targetPath) throws IOException {
        unzip(zipFilePath, targetPath, true);
    }

    public static void unzip(String zipFilePath, String targetPath, boolean safeUnzip) throws IOException {
        ZipFile zipFile = new ZipFile(zipFilePath);
        try {
            Enumeration<?> entryEnum = zipFile.entries();
            if (null != entryEnum) {
                while (entryEnum.hasMoreElements()) {
                    OutputStream os = null;
                    InputStream is = null;
                    try {
                        ZipEntry zipEntry = (ZipEntry) entryEnum.nextElement();
                        if (!zipEntry.isDirectory()) {
                            if (safeUnzip && isNotSafeFile(zipEntry.getName())) {
                                continue;
                            }
                            File targetFile = new File(targetPath + File.separator + zipEntry.getName());
                            if (!targetFile.getParentFile().exists()) {
                                targetFile.getParentFile().mkdirs();
                            }
                            os = new BufferedOutputStream(new FileOutputStream(targetFile));
                            is = zipFile.getInputStream(zipEntry);
                            byte[] buffer = new byte[4096];
                            int readLen = 0;
                            while ((readLen = is.read(buffer, 0, 4096)) > 0) {
                                os.write(buffer, 0, readLen);
                            }
                        }
                    } finally {
                        close(is, os);
                    }
                }
            }
        } finally {
            zipFile.close();
        }
    }

    private static boolean isNotSafeFile(String name) {
        name = name.toLowerCase();
        return name.contains("..") || name.endsWith(".jsp") || name.endsWith(".jspx");
    }

}
@@ -690,16 +690,46 @@
 JFinal;%0A
+import com.jfinal.kit.LogKit;%0A
 import c
@@ -1953,32 +1953,75 @@
 (Exception e) %7B%0A
+            LogKit.error(e.toString(), e);%0A
         %7D finall
@@ -2365,32 +2365,75 @@
 (Exception e) %7B%0A
+            LogKit.error(e.toString(), e);%0A
         %7D finall
@@ -2458,14 +2458,8 @@
 ose(
-null, 
 fos)
@@ -2474,38 +2474,37 @@
   %7D%0A    %7D%0A%0A    p
-rivate
+ublic
  static void clo
@@ -2510,39 +2510,132 @@
 ose(
-InputStream is, OutputStream os
+Closeable... closeable) %7B%0A        if (closeable != null %7C%7C closeable.length != 0) %7B%0A            for (Closeable c : closeable
 ) %7B%0A
@@ -2646,14 +2646,21 @@
     
+  
+      
 if (
-is
+c
  != 
@@ -2671,32 +2671,40 @@
 ) %7B%0A            
+ 
+       
 try %7B%0A          
@@ -2709,18 +2709,25 @@
         
-is
+        c
 .close()
@@ -2732,32 +2732,40 @@
 ();%0A            
+  
+      
 %7D catch (IOExcep
@@ -2782,34 +2782,32 @@
             
-%7D%0A
         
 %7D%0A        if
@@ -2798,35 +2798,42 @@
     
-%7D%0A
     
-    if (os != null) %7B
+LogKit.error(e.toString(), e);
 %0A   
@@ -2834,33 +2834,24 @@
 );%0A         
-   try %7B%0A
             
@@ -2853,62 +2853,27 @@
     
+%7D%0A
      
-os.close();%0A            %7D catch (IOException e) %7B
+           %7D
 %0A   
@@ -2892,32 +2892,33 @@
        %7D%0A    %7D%0A%0A
+%0A
     public stati
@@ -3100,32 +3100,33 @@
 , true);%0A    %7D%0A%0A
+%0A
     public stati
@@ -3243,32 +3243,33 @@
 , true);%0A    %7D%0A%0A
+%0A
     public stati
@@ -4877,22 +4877,21 @@
     
+close(
 zipFile
-.close(
 );%0A 

optimize FileUtil