Helius
2022-07-04 2736d16a8b1804f7291a56a147f76ef3584d6619
src/main/java/com/xcong/farmer/cms/common/utils/FileUtils.java
@@ -1,10 +1,10 @@
package com.xcong.farmer.cms.common.utils;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
 * @author wzy
@@ -30,16 +30,75 @@
        return dir + fileName;
    }
    public static void zipUpload(File file, String templateDir, String staticDir) throws IOException {
        ZipFile zipFile = new ZipFile(file);
    public static void zipUpload(String zipPath, String templateDir) throws IOException {
        FileInputStream fis = new FileInputStream(zipPath);
        ZipInputStream zipIs = new ZipInputStream(fis);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        if (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (zipEntry.isDirectory()) {
//                zipEntry.
        ZipEntry ze = null;
        try {
            while ((ze = zipIs.getNextEntry()) != null) {
                File zfile = new File(path(templateDir, ze.getName()));
                if (ze.isDirectory()) {
                    if (!zfile.exists()) {
                        zfile.mkdirs();
                    }
                    zipIs.closeEntry();
                } else {
                    byte[] data = new byte[1024];
                    FileOutputStream fos = new FileOutputStream(zfile);
                    int i = 0;
                    while ((i = zipIs.read(data)) != -1) {
                        fos.write(data, 0, i);
                    }
                    zipIs.closeEntry();
                    fos.close();
                }
            }
            System.out.println(zipEntry.getName());
            fis.close();
            zipIs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void upload(File file, String dir) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            for (File dirFile : files) {
                upload(dirFile, path(dir, dirFile.getName()));
            }
        } else {
            outputFile(file, dir);
        }
    }
    public static void outputFile(File file, String outputDir) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            int count;
            int buffer = 1024;
            byte[] dataByte = new byte[buffer];
            FileOutputStream out = new FileOutputStream(path(outputDir, file.getName()));
            BufferedOutputStream bos = new BufferedOutputStream(out, buffer);
            while((count = bis.read(dataByte, 0, buffer)) != -1) {
                bos.write(dataByte, 0 ,count);
            }
            bos.flush();
            bos.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}