|
@@ -0,0 +1,899 @@
|
|
|
|
+package cn.com.qmth.am.utils;
|
|
|
|
+
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.DataInputStream;
|
|
|
|
+import java.io.DataOutputStream;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.MalformedURLException;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Enumeration;
|
|
|
|
+import java.util.LinkedList;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
|
+import java.util.zip.ZipFile;
|
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
|
+
|
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
|
+
|
|
|
|
+@SuppressWarnings("restriction")
|
|
|
|
+public class FileUtil {
|
|
|
|
+
|
|
|
|
+ public static void deleteDirectory(File dirFile) {
|
|
|
|
+ if (!dirFile.exists()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (dirFile.isFile()) {
|
|
|
|
+ dirFile.delete();
|
|
|
|
+ } else {
|
|
|
|
+ File[] files = dirFile.listFiles();
|
|
|
|
+ if (files != null) {
|
|
|
|
+ for (int i = 0; i < files.length; i++) {
|
|
|
|
+ deleteDirectory(files[i]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ dirFile.delete();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static boolean saveFileByUrl(String fileUrl, File file) {
|
|
|
|
+ URL url;
|
|
|
|
+ try {
|
|
|
|
+ url = new URL(fileUrl);
|
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
|
+ throw new RuntimeException("文件链接错误:" + fileUrl, e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ HttpURLConnection connection;
|
|
|
|
+ try {
|
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException("下载出错", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (DataInputStream dataInputStream = new DataInputStream(connection.getInputStream());
|
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(file);
|
|
|
|
+ DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);) {
|
|
|
|
+
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
|
+ int count;
|
|
|
|
+ while ((count = dataInputStream.read(buffer)) > 0) {
|
|
|
|
+ dataOutputStream.write(buffer, 0, count);
|
|
|
|
+ }
|
|
|
|
+ fileOutputStream.flush();
|
|
|
|
+ dataOutputStream.flush();
|
|
|
|
+ return true;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException("下载出错", e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (connection != null) {
|
|
|
|
+ connection.disconnect();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static byte[] base64ToByte(String base64Str) {
|
|
|
|
+ if (base64Str.contains("data:image")) {// base64图片
|
|
|
|
+ base64Str = base64Str.substring(base64Str.indexOf(",") + 1).replaceAll("%0A", "");
|
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
|
+ try {
|
|
|
|
+ return decoder.decodeBuffer(base64Str);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw new RuntimeException("not base64 string");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void base64ToFile(File file, String base64Str) {
|
|
|
|
+ if (base64Str.contains("data:image")) {// base64图片
|
|
|
|
+ base64Str = base64Str.substring(base64Str.indexOf(",") + 1).replaceAll("%0A", "");
|
|
|
|
+ BASE64Decoder decoder = new BASE64Decoder();
|
|
|
|
+ try {
|
|
|
|
+ byte[] bytes = decoder.decodeBuffer(base64Str);
|
|
|
|
+ FileUtils.writeByteArrayToFile(file, bytes);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw new RuntimeException("not base64 string");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String fileToBase64(InputStream is, String suff) {
|
|
|
|
+ byte[] base64Byte;
|
|
|
|
+ try {
|
|
|
|
+ base64Byte = new byte[0];
|
|
|
|
+ byte[] imgByte;
|
|
|
|
+ imgByte = IOUtils.toByteArray(is);
|
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (is != null) {
|
|
|
|
+ try {
|
|
|
|
+ is.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "data:image/" + suff + ";base64," + new String(base64Byte);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String byteToBase64(byte[] imgByte, String suff) {
|
|
|
|
+ InputStream is = null;
|
|
|
|
+ byte[] base64Byte = new byte[0];
|
|
|
|
+ try {
|
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
+ } finally {
|
|
|
|
+ if (is != null) {
|
|
|
|
+ try {
|
|
|
|
+ is.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "data:image/" + suff + ";base64," + new String(base64Byte);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String fileToBase64(File imgFile) {
|
|
|
|
+ String fileName = imgFile.getName();
|
|
|
|
+ String suff = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
|
+ InputStream is = null;
|
|
|
|
+ byte[] base64Byte;
|
|
|
|
+ try {
|
|
|
|
+ base64Byte = new byte[0];
|
|
|
|
+ byte[] imgByte;
|
|
|
|
+ is = new FileInputStream(imgFile);
|
|
|
|
+ imgByte = IOUtils.toByteArray(is);
|
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (is != null) {
|
|
|
|
+ try {
|
|
|
|
+ is.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "data:image/" + suff + ";base64," + new String(base64Byte);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 下载服务器上的文件
|
|
|
|
+ *
|
|
|
|
+ * @param filename
|
|
|
|
+ * 文件名称
|
|
|
|
+ * @param fullFilePath
|
|
|
|
+ * 文件全路径
|
|
|
|
+ * @param response
|
|
|
|
+ */
|
|
|
|
+ public static void downloadFile(String filename, String fullFilePath, HttpServletResponse response) {
|
|
|
|
+ try (InputStream in = new FileInputStream(fullFilePath); OutputStream out = response.getOutputStream();) {
|
|
|
|
+
|
|
|
|
+ // 设置编码
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
+
|
|
|
|
+ // 设置Content-Disposition,名称强制为UTF-8
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename="
|
|
|
|
+ + URLEncoder.encode(filename, "UTF-8").replace("%28", "(").replace("%29", ")"));
|
|
|
|
+ response.setHeader("Accept-Length", String.valueOf(in.available()));
|
|
|
|
+
|
|
|
|
+ // 设置强制下载不打开
|
|
|
|
+ response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
|
+
|
|
|
|
+ // 读取目标文件,通过response将目标文件写到客户端
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
|
+ int count;
|
|
|
|
+ while ((count = in.read(buffer)) > 0) {
|
|
|
|
+ out.write(buffer, 0, count);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ response.flushBuffer();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException("下载出错:" + e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获得文件MIME类型
|
|
|
|
+ *
|
|
|
|
+ * @param filename
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String getContentType(String filename) {
|
|
|
|
+ String type = null;
|
|
|
|
+ Path path = Paths.get(filename);
|
|
|
|
+ try {
|
|
|
|
+ type = Files.probeContentType(path);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException("出错:" + e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ return type;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
|
|
|
|
+ *
|
|
|
|
+ * @param sourceFilePath
|
|
|
|
+ * :待压缩的文件夹路径
|
|
|
|
+ * @param zipFilePath
|
|
|
|
+ * :压缩后zip文件的存放路径
|
|
|
|
+ * @param fileName
|
|
|
|
+ * :zip文件的名称
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static void fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
|
|
|
|
+
|
|
|
|
+ File sourceFile = new File(sourceFilePath);
|
|
|
|
+ if (!sourceFile.exists()) {
|
|
|
|
+ throw new RuntimeException("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ File zipFile = new File(zipFilePath + File.separator + fileName + ".zip");
|
|
|
|
+ if (zipFile.exists()) {
|
|
|
|
+ throw new RuntimeException(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ File[] sourceFiles = sourceFile.listFiles();
|
|
|
|
+ if (null == sourceFiles || sourceFiles.length < 1) {
|
|
|
|
+ throw new RuntimeException("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(zipFile);
|
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(bos);) {
|
|
|
|
+
|
|
|
|
+ byte[] bytes = new byte[1024 * 10];
|
|
|
|
+ for (int i = 0; i < sourceFiles.length; i++) {
|
|
|
|
+ File file = sourceFiles[i];
|
|
|
|
+ if (!file.isFile()) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (FileInputStream fis = new FileInputStream(file);
|
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10);) {
|
|
|
|
+ // 创建ZIP实体,并添加进压缩包
|
|
|
|
+ String fileEncode = System.getProperty("file.encoding");
|
|
|
|
+ String name = new String(file.getName().getBytes(), fileEncode);
|
|
|
|
+
|
|
|
|
+ ZipEntry zipEntry = new ZipEntry(name);
|
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
|
+
|
|
|
|
+ // 读取待压缩的文件并写进压缩包里
|
|
|
|
+ int read;
|
|
|
|
+ while ((read = bis.read(bytes, 0, 1024 * 10)) != -1) {
|
|
|
|
+ zos.write(bytes, 0, read);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ zos.flush();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException("出错:" + e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException("出错:" + e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void createDirectory(String downloadDirectory) {
|
|
|
|
+ File directory = new File(downloadDirectory);
|
|
|
|
+ if (!directory.exists()) {
|
|
|
|
+ directory.mkdirs();
|
|
|
|
+ } else {
|
|
|
|
+ FileUtils.deleteQuietly(directory);
|
|
|
|
+ directory.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static File createZip(String sourceFilePath, String targetFilePath) throws IOException {
|
|
|
|
+ OutputStream fos = null;
|
|
|
|
+ ZipOutputStream zos = null;
|
|
|
|
+ try {
|
|
|
|
+ File zipfile = new File(targetFilePath);
|
|
|
|
+ zipfile.deleteOnExit();
|
|
|
|
+ fos = new FileOutputStream(zipfile);
|
|
|
|
+ zos = new ZipOutputStream(fos);
|
|
|
|
+ // zos.setEncoding("utf-8"); // Solve linxu's mess
|
|
|
|
+ writeZip(new File(sourceFilePath), null, zos);
|
|
|
|
+ return zipfile;
|
|
|
|
+ } finally {
|
|
|
|
+ if (zos != null) {
|
|
|
|
+ zos.close();
|
|
|
|
+ }
|
|
|
|
+ if (fos != null) {
|
|
|
|
+ fos.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void writeZip(File file, String parentPath, ZipOutputStream zos) throws IOException {
|
|
|
|
+ if (file.exists()) {
|
|
|
|
+ ZipEntry ze = null;
|
|
|
|
+ if (file.isDirectory()) {// Processing folder
|
|
|
|
+ if (parentPath == null) {
|
|
|
|
+ parentPath = "";
|
|
|
|
+ } else {
|
|
|
|
+ parentPath += file.getName() + "/";
|
|
|
|
+ }
|
|
|
|
+ File[] files = file.listFiles();
|
|
|
|
+ if (files != null) {
|
|
|
|
+ for (File f : files) {
|
|
|
|
+ writeZip(f, parentPath, zos);
|
|
|
|
+ }
|
|
|
|
+ } else { // An empty directory creates the current directory
|
|
|
|
+ try {
|
|
|
|
+ ze = new ZipEntry(parentPath);
|
|
|
|
+ // ze.setUnixMode(755);// Solve Linux mess file Settings
|
|
|
|
+ // 644 directory Settings 755
|
|
|
|
+ zos.putNextEntry(ze);
|
|
|
|
+
|
|
|
|
+ zos.flush();
|
|
|
|
+ } finally {
|
|
|
|
+ if (zos != null) {
|
|
|
|
+ zos.closeEntry();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ FileInputStream fis = null;
|
|
|
|
+ try {
|
|
|
|
+ fis = new FileInputStream(file);
|
|
|
|
+ ze = new ZipEntry(parentPath + file.getName());
|
|
|
|
+ // ze.setUnixMode(644);// Solve Linux mess file Settings 644
|
|
|
|
+ // directory Settings 755
|
|
|
|
+ zos.putNextEntry(ze);
|
|
|
|
+ byte[] content = new byte[1024];
|
|
|
|
+ int len;
|
|
|
|
+ while ((len = fis.read(content)) != -1) {
|
|
|
|
+ zos.write(content, 0, len);
|
|
|
|
+ zos.flush();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } finally {
|
|
|
|
+ if (fis != null) {
|
|
|
|
+ fis.close();
|
|
|
|
+ }
|
|
|
|
+ if (zos != null) {
|
|
|
|
+ zos.closeEntry();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 文件压缩
|
|
|
|
+ *
|
|
|
|
+ * @param target
|
|
|
|
+ * 目录或文件
|
|
|
|
+ * @param zipFile
|
|
|
|
+ * 压缩后的ZIP文件
|
|
|
|
+ */
|
|
|
|
+ public static boolean doZip(File target, File zipFile) {
|
|
|
|
+ if (target == null || !target.exists()) {
|
|
|
|
+ throw new RuntimeException("目录或文件不能为空!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (zipFile == null) {
|
|
|
|
+ throw new RuntimeException("待压缩的文件不能为空!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (OutputStream outStream = new FileOutputStream(zipFile);
|
|
|
|
+ ZipOutputStream zipOutStream = new ZipOutputStream(outStream, Charset.forName("UTF-8"));) {
|
|
|
|
+ if (!zipFile.exists()) {
|
|
|
|
+ boolean ok = zipFile.createNewFile();
|
|
|
|
+ if (!ok) {
|
|
|
|
+ throw new RuntimeException("压缩的文件创建失败!");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (target.isDirectory()) {
|
|
|
|
+ File[] files = target.listFiles();
|
|
|
|
+ if (files.length == 0) {
|
|
|
|
+ throw new RuntimeException("文件夹内未找到任何文件!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (File file : files) {
|
|
|
|
+ doZip(zipOutStream, file, null);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ doZip(zipOutStream, target, null);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void doZip(ZipOutputStream zipOutStream, File target, String parentDir) throws IOException {
|
|
|
|
+ // log.info("Zip:" + parentDir);
|
|
|
|
+ if (parentDir == null) {
|
|
|
|
+ parentDir = "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!"".equals(parentDir) && !parentDir.endsWith(File.separator)) {
|
|
|
|
+ parentDir += File.separator;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (target.isDirectory()) {
|
|
|
|
+ File[] files = target.listFiles();
|
|
|
|
+ if (files.length > 0) {
|
|
|
|
+ for (File file : files) {
|
|
|
|
+ doZip(zipOutStream, file, parentDir + target.getName());
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
|
|
|
|
+ zipOutStream.closeEntry();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ try (InputStream is = new FileInputStream(target);) {
|
|
|
|
+ zipOutStream.putNextEntry(new ZipEntry(parentDir + target.getName()));
|
|
|
|
+ int len;
|
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
+ while ((len = is.read(bytes)) > 0) {
|
|
|
|
+ zipOutStream.write(bytes, 0, len);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ zipOutStream.closeEntry();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 解压文件
|
|
|
|
+ *
|
|
|
|
+ * @param targetDir
|
|
|
|
+ * 解压目录
|
|
|
|
+ * @param zipFile
|
|
|
|
+ * 待解压的ZIP文件
|
|
|
|
+ */
|
|
|
|
+ public static List<File> unZip(File targetDir, File zipFile) {
|
|
|
|
+ if (targetDir == null) {
|
|
|
|
+ throw new RuntimeException("解压目录不能为空!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (zipFile == null) {
|
|
|
|
+ throw new RuntimeException("待解压的文件不能为空!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!zipFile.exists()) {
|
|
|
|
+ throw new RuntimeException("待解压的文件不存在!" + zipFile.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String zipName = zipFile.getName().toLowerCase();
|
|
|
|
+ if (zipFile.isDirectory() || zipName.indexOf(".zip") < 0) {
|
|
|
|
+ throw new RuntimeException("待解压的文件格式错误!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!targetDir.exists()) {
|
|
|
|
+ targetDir.mkdir();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<File> result = new LinkedList<>();
|
|
|
|
+
|
|
|
|
+ try (ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));) {
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
|
+ Enumeration entries = zip.entries();
|
|
|
|
+ while (entries.hasMoreElements()) {
|
|
|
|
+ ZipEntry entry = (ZipEntry) entries.nextElement();
|
|
|
|
+
|
|
|
|
+ // Linux中需要替换掉路径的反斜杠
|
|
|
|
+ String entryName = (File.separator + entry.getName()).replaceAll("\\\\", "/");
|
|
|
|
+
|
|
|
|
+ String filePath = targetDir.getAbsolutePath() + entryName;
|
|
|
|
+ File target = new File(filePath);
|
|
|
|
+ if (entry.isDirectory()) {
|
|
|
|
+ target.mkdirs();
|
|
|
|
+ } else {
|
|
|
|
+ File dir = target.getParentFile();
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (OutputStream os = new FileOutputStream(target); InputStream is = zip.getInputStream(entry);) {
|
|
|
|
+ IOUtils.copy(is, os);
|
|
|
|
+ os.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ result.add(target);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void unZipFiles(String zipFileName, String targetDirName) throws IOException {
|
|
|
|
+ if (!targetDirName.endsWith(File.separator)) {
|
|
|
|
+ targetDirName = targetDirName + File.separator;
|
|
|
|
+ }
|
|
|
|
+ ZipFile zipFile = null;
|
|
|
|
+ try {
|
|
|
|
+ // Create the ZipFile object from the ZIP file
|
|
|
|
+ zipFile = new ZipFile(zipFileName);
|
|
|
|
+ ZipEntry entry = null;
|
|
|
|
+ String entryName = null;
|
|
|
|
+ String descFileDir = null;
|
|
|
|
+ byte[] buf = new byte[4096];
|
|
|
|
+ int readByte = 0;
|
|
|
|
+ // Gets all entry in the ZIP file
|
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
|
+ Enumeration enums = zipFile.entries();
|
|
|
|
+ // Go through all entry
|
|
|
|
+ while (enums.hasMoreElements()) {
|
|
|
|
+ entry = (ZipEntry) enums.nextElement();
|
|
|
|
+ // Get the name entry
|
|
|
|
+ entryName = entry.getName();
|
|
|
|
+ descFileDir = targetDirName + entryName;
|
|
|
|
+ if (entry.isDirectory()) {
|
|
|
|
+ // If entry is a directory, create the directory
|
|
|
|
+ // entry.setUnixMode(755);// Solve Linux mess file Settings
|
|
|
|
+ // 644 directory Settings 755
|
|
|
|
+ new File(descFileDir).mkdirs();
|
|
|
|
+ continue;
|
|
|
|
+ } else {
|
|
|
|
+ // If entry is a file, the parent directory is created
|
|
|
|
+ // entry.setUnixMode(644);//Solve Linux mess file Settings
|
|
|
|
+ // 644 directory Settings 755
|
|
|
|
+ new File(descFileDir).getParentFile().mkdirs();
|
|
|
|
+ }
|
|
|
|
+ File file = new File(descFileDir);
|
|
|
|
+ // Open the file output stream
|
|
|
|
+ OutputStream os = null;
|
|
|
|
+ // Open the entry input stream from the ZipFile object
|
|
|
|
+ InputStream is = null;
|
|
|
|
+ try {
|
|
|
|
+ os = new FileOutputStream(file);
|
|
|
|
+ is = zipFile.getInputStream(entry);
|
|
|
|
+ while ((readByte = is.read(buf)) != -1) {
|
|
|
|
+ os.write(buf, 0, readByte);
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ if (os != null)
|
|
|
|
+ os.close();
|
|
|
|
+ if (is != null)
|
|
|
|
+ is.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ if (zipFile != null)
|
|
|
|
+ zipFile.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void deleteFolder(String path) {
|
|
|
|
+
|
|
|
|
+ File file = new File(path);
|
|
|
|
+ if (file.exists()) {
|
|
|
|
+ if (file.isFile()) {
|
|
|
|
+ deleteFile(path);
|
|
|
|
+ } else {
|
|
|
|
+ deleteDirectory(path);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void deleteFile(String path) {
|
|
|
|
+ File file = new File(path);
|
|
|
|
+ if (file.isFile() && file.exists()) {
|
|
|
|
+ file.delete();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void deleteDirectory(String path) {
|
|
|
|
+ if (!path.endsWith(File.separator)) {
|
|
|
|
+ path = path + File.separator;
|
|
|
|
+ }
|
|
|
|
+ File dirFile = new File(path);
|
|
|
|
+ if (!dirFile.exists() || !dirFile.isDirectory()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ File[] files = dirFile.listFiles();
|
|
|
|
+ if (files != null) {
|
|
|
|
+ for (int i = 0; i < files.length; i++) {
|
|
|
|
+ if (files[i].isFile()) {
|
|
|
|
+ deleteFile(files[i].getAbsolutePath());
|
|
|
|
+ } else {
|
|
|
|
+ deleteDirectory(files[i].getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dirFile.delete();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static File cutFile(String sourcePath, String targetPath, int n) {
|
|
|
|
+ File file = new File(sourcePath);
|
|
|
|
+ File newFile = new File(targetPath);
|
|
|
|
+
|
|
|
|
+ try (FileInputStream fis = new FileInputStream(file);
|
|
|
|
+ InputStream is = new BufferedInputStream(fis);
|
|
|
|
+ OutputStream os = new FileOutputStream(newFile);) {
|
|
|
|
+
|
|
|
|
+ // 从n个字节开始读,注意中文是两个字节
|
|
|
|
+ fis.skip(n);
|
|
|
|
+
|
|
|
|
+ // 指定文件位置读取的文件流,存入新文件
|
|
|
|
+ byte buffer[] = new byte[4 * 1024];
|
|
|
|
+ int len;
|
|
|
|
+ while ((len = is.read(buffer)) != -1) {
|
|
|
|
+ os.write(buffer, 0, len);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ os.flush();
|
|
|
|
+ return newFile;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取文件前面部分N个字节
|
|
|
|
+ *
|
|
|
|
+ * @param path
|
|
|
|
+ * 文件路径
|
|
|
|
+ * @param headerSize
|
|
|
|
+ * 头信息字节数(必须2的倍数)
|
|
|
|
+ * @param signSize
|
|
|
|
+ * 签名信息字节数
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String[] readFileHeader(String path, int headerSize, int signSize) {
|
|
|
|
+ int n = headerSize / 2;
|
|
|
|
+ String[] codes = new String[n + 1];
|
|
|
|
+
|
|
|
|
+ File file = new File(path);
|
|
|
|
+ try (FileInputStream fis = new FileInputStream(file); DataInputStream ois = new DataInputStream(fis);) {
|
|
|
|
+ // 分n次读取文件(n * 2)个字节
|
|
|
|
+ for (int i = 0; i < n; i++) {
|
|
|
|
+ codes[i] = String.valueOf(ois.readShort());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (signSize > 0) {
|
|
|
|
+ StringBuilder ss = new StringBuilder();
|
|
|
|
+ for (int i = 0; i < signSize; i++) {
|
|
|
|
+ ss.append((char) ois.readByte());
|
|
|
|
+ }
|
|
|
|
+ codes[2] = ss.toString();
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return codes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取文件内容
|
|
|
|
+ *
|
|
|
|
+ * @param file
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @SuppressWarnings("deprecation")
|
|
|
|
+ public static String readFileContent(File file) {
|
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
|
+ InputStreamReader streamReader = null;
|
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
|
+ try {
|
|
|
|
+ String encoding = "UTF-8";
|
|
|
|
+ if (file.exists() && file.isFile()) {
|
|
|
|
+ streamReader = new InputStreamReader(new FileInputStream(file), encoding);
|
|
|
|
+ bufferedReader = new BufferedReader(streamReader);
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
|
+ content.append(line);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(streamReader);
|
|
|
|
+ IOUtils.closeQuietly(bufferedReader);
|
|
|
|
+ }
|
|
|
|
+ return content.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("deprecation")
|
|
|
|
+ public static String readFileContent(InputStream in) {
|
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
|
+ InputStreamReader streamReader = null;
|
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
|
+ try {
|
|
|
|
+ String encoding = "UTF-8";
|
|
|
|
+ streamReader = new InputStreamReader(in, encoding);
|
|
|
|
+ bufferedReader = new BufferedReader(streamReader);
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
|
+ content.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
|
+ IOUtils.closeQuietly(streamReader);
|
|
|
|
+ IOUtils.closeQuietly(bufferedReader);
|
|
|
|
+ }
|
|
|
|
+ return content.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成日期目录路径
|
|
|
|
+ */
|
|
|
|
+ public static String generateDateDir() {
|
|
|
|
+ return "/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String generateFileName() {
|
|
|
|
+ return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String generateDateName() {
|
|
|
|
+ return new SimpleDateFormat("yyMMddHHmmss").format(new Date());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取文件后缀名(包含".")
|
|
|
|
+ */
|
|
|
|
+ public static String getFileSuffix(String fileName) {
|
|
|
|
+ if (fileName == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ int index = fileName.lastIndexOf(".");
|
|
|
|
+ if (index > -1) {
|
|
|
|
+ return fileName.substring(index).toLowerCase();
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取无后缀的文件名
|
|
|
|
+ *
|
|
|
|
+ * @param fileName
|
|
|
|
+ * 示例:../xxx/abc.xx
|
|
|
|
+ * @return 示例:../xxx/abc
|
|
|
|
+ */
|
|
|
|
+ public static String getFilePathName(String fileName) {
|
|
|
|
+ if (fileName != null && fileName.length() > 0) {
|
|
|
|
+ int index = fileName.lastIndexOf(".");
|
|
|
|
+ if (index != -1) {
|
|
|
|
+ return fileName.substring(0, index);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建文件目录
|
|
|
|
+ */
|
|
|
|
+ public static boolean makeDirs(String path) {
|
|
|
|
+ if (path == null || "".equals(path)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ File folder = new File(path);
|
|
|
|
+ if (!folder.exists()) {
|
|
|
|
+ return folder.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void dozip(File target, File[] files) {
|
|
|
|
+ // 压缩后的 zip 文件名
|
|
|
|
+ if (target == null || !target.exists()) {
|
|
|
|
+ throw new RuntimeException("目录或文件不能为空!");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 创建 ZipOutputStream 对象
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(target);
|
|
|
|
+ ZipOutputStream zipOut = new ZipOutputStream(fos, Charset.forName("UTF-8"));
|
|
|
|
+
|
|
|
|
+ // 循环每个文件并将其添加到压缩包
|
|
|
|
+ for (File vo : files) {
|
|
|
|
+ FileInputStream in = new FileInputStream(vo);
|
|
|
|
+ try {
|
|
|
|
+ ZipEntry zipEntry = new ZipEntry(vo.getName());
|
|
|
|
+ zipOut.putNextEntry(zipEntry);
|
|
|
|
+ // 将文件内容写入 ZipOutputStream
|
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
+ int length;
|
|
|
|
+ while ((length = in.read(bytes)) >= 0) {
|
|
|
|
+ zipOut.write(bytes, 0, length);
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ // 关闭当前文件的输入流
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 关闭 ZipOutputStream
|
|
|
|
+ zipOut.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void dozip(File target, List<File> files) {
|
|
|
|
+ // 压缩后的 zip 文件名
|
|
|
|
+ if (target == null || !target.exists()) {
|
|
|
|
+ throw new RuntimeException("目录或文件不能为空!");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 创建 ZipOutputStream 对象
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(target);
|
|
|
|
+ ZipOutputStream zipOut = new ZipOutputStream(fos, Charset.forName("UTF-8"));
|
|
|
|
+
|
|
|
|
+ // 循环每个文件并将其添加到压缩包
|
|
|
|
+ for (File vo : files) {
|
|
|
|
+ FileInputStream in = new FileInputStream(vo);
|
|
|
|
+ try {
|
|
|
|
+ ZipEntry zipEntry = new ZipEntry(vo.getName());
|
|
|
|
+ zipOut.putNextEntry(zipEntry);
|
|
|
|
+ // 将文件内容写入 ZipOutputStream
|
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
+ int length;
|
|
|
|
+ while ((length = in.read(bytes)) >= 0) {
|
|
|
|
+ zipOut.write(bytes, 0, length);
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ // 关闭当前文件的输入流
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 关闭 ZipOutputStream
|
|
|
|
+ zipOut.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @SuppressWarnings("deprecation")
|
|
|
|
+ public static void donwLoadFile(HttpServletResponse response, String fileName, InputStream in) {
|
|
|
|
+ OutputStream out = null;
|
|
|
|
+ try {
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
+ out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
+ IOUtils.copy(in, out);
|
|
|
|
+ out.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|