From 05f3eec8ba39fb05eac09d8d3f2930cd77dd41f7 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Tue, 12 Jul 2022 11:39:30 +0800 Subject: [PATCH] 20220606 --- src/main/java/com/xcong/farmer/cms/common/utils/FileUtils.java | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/xcong/farmer/cms/common/utils/FileUtils.java b/src/main/java/com/xcong/farmer/cms/common/utils/FileUtils.java index 005a65c..32d768d 100644 --- a/src/main/java/com/xcong/farmer/cms/common/utils/FileUtils.java +++ b/src/main/java/com/xcong/farmer/cms/common/utils/FileUtils.java @@ -1,7 +1,104 @@ -package com.xcong.farmer.cms.common.utils;/** -* -* @author wzy -* @date 2022-07-04 -**/ +package com.xcong.farmer.cms.common.utils; + +import java.io.*; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; + +/** + * @author wzy + * @date 2022-07-04 + **/ public class FileUtils { + + public static String path(String path) { + if (!path.endsWith("/")) { + return path + "/"; + } + + return path; + } + + public static String path(String path, String fileName) { + File file = new File(path); + if (!file.isDirectory()){ + return ""; + } + + String dir = path(path); + return dir + fileName; + } + + public static void zipUpload(String zipPath, String templateDir) throws IOException { + FileInputStream fis = new FileInputStream(zipPath); + ZipInputStream zipIs = new ZipInputStream(fis); + + 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(); + } + } + + 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(); + } + } } -- Gitblit v1.9.1