|
@@ -0,0 +1,780 @@
|
|
|
|
+package cn.com.qmth.export;
|
|
|
|
+
|
|
|
|
+import java.io.BufferedInputStream;
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.BufferedWriter;
|
|
|
|
+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.io.OutputStreamWriter;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.MalformedURLException;
|
|
|
|
+import java.net.URL;
|
|
|
|
+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 org.apache.commons.codec.binary.Base64;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
|
+
|
|
|
|
+import sun.misc.BASE64Decoder;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author chenken
|
|
|
|
+ * @date 2017年7月17日 上午9:36:32
|
|
|
|
+ * @company QMTH
|
|
|
|
+ */
|
|
|
|
+public class FileUtil {
|
|
|
|
+ public static void clearDirectory(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());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void writeFile(String dir,String name,String content) throws IOException {
|
|
|
|
+ BufferedWriter out=null;
|
|
|
|
+ try {
|
|
|
|
+ File writename = new File(dir+name);
|
|
|
|
+ writename.createNewFile();
|
|
|
|
+ out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(writename), "UTF-8"));
|
|
|
|
+ out.write(content);
|
|
|
|
+ out.flush();
|
|
|
|
+ } finally {
|
|
|
|
+ if(out!=null) {
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void base64ToFile(File file,String base64Str){
|
|
|
|
+ if (base64Str.contains("data:image")) {//base64图片
|
|
|
|
+ base64Str = base64Str.substring(base64Str.indexOf(",") + 1);
|
|
|
|
+ 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 fileToBase64Src(File imgFile){
|
|
|
|
+ String fn=imgFile.getName();
|
|
|
|
+ String suff=fn.substring(fn.lastIndexOf(".")+1).toLowerCase();
|
|
|
|
+ return "data:image/"+suff+";base64,"+fileToBase64(imgFile);
|
|
|
|
+ }
|
|
|
|
+ public static String fileToBase64(File imgFile){
|
|
|
|
+ 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 new String(base64Byte);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将网络文件保存到本地
|
|
|
|
+ *
|
|
|
|
+ * @param fileUrl
|
|
|
|
+ * 网络文件URL
|
|
|
|
+ * @param localFilePath
|
|
|
|
+ * 例如D:/123.txt
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean saveUrlAs(String fileUrl, String localFilePath) {
|
|
|
|
+ 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.getMessage(),e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (DataInputStream dataInputStream = new DataInputStream(connection.getInputStream());
|
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(localFilePath);
|
|
|
|
+ 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.getMessage(),e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (connection != null) {
|
|
|
|
+ connection.disconnect();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获得文件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), "UTF-8");
|
|
|
|
+
|
|
|
|
+ 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.separator;
|
|
|
|
+ }
|
|
|
|
+ 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("UTF-8"));) {
|
|
|
|
+
|
|
|
|
+ @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 String getDocxBasePath() {
|
|
|
|
+ String path = FileUtil.class.getResource("/").getPath() + "templates/docx";
|
|
|
|
+ return path;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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
|
|
|
|
+ */
|
|
|
|
+ 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();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成日期目录路径
|
|
|
|
+ */
|
|
|
|
+ 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 saveAsFile(String path, String content) {
|
|
|
|
+ saveAsFile(path, content, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void saveAsFile(String path, String content, String encoding) {
|
|
|
|
+ if (path == null || content == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (encoding == null) {
|
|
|
|
+ encoding = "UTF-8";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ File file = new File(path);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ if (makeDirs(file.getParent())) {
|
|
|
|
+ boolean ok;
|
|
|
|
+ try {
|
|
|
|
+ ok = file.createNewFile();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException("文件创建失败!");
|
|
|
|
+ }
|
|
|
|
+ if (!ok) {
|
|
|
|
+ throw new RuntimeException("文件创建失败!");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
+ OutputStreamWriter write = new OutputStreamWriter(fos, encoding);
|
|
|
|
+ BufferedWriter bw = new BufferedWriter(write);
|
|
|
|
+ ) {
|
|
|
|
+ bw.write(content);
|
|
|
|
+ bw.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException("文件创建失败!",e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|