|
|
@@ -0,0 +1,166 @@
|
|
|
+package com.xjrsoft.common.utils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author dzx
|
|
|
+ * @Description 文件压缩工具
|
|
|
+ * 2023/12/5
|
|
|
+ */
|
|
|
+public class FileZipUtil {
|
|
|
+ public static ByteArrayInputStream inputStream2ByteArray(InputStream in, boolean isClose) {
|
|
|
+ byte[] byteArray = null;
|
|
|
+ try {
|
|
|
+ int total = in.available();
|
|
|
+ byteArray = new byte[total];
|
|
|
+ in.read(byteArray);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (isClose) {
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (Exception e2) {
|
|
|
+ //zou.log.Logger.getServerlog().error(" ر ʧ ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new ByteArrayInputStream(byteArray);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] byteArray(InputStream in, boolean isClose) {
|
|
|
+ byte[] byteArray = null;
|
|
|
+ try {
|
|
|
+ int total = in.available();
|
|
|
+ byteArray = new byte[total];
|
|
|
+ in.read(byteArray);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (isClose) {
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (Exception e2) {
|
|
|
+ //zou.log.Logger.getServerlog().error(" ر ʧ ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return byteArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void zipFiles(File[] srcfile, File zipfile) {
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ try {
|
|
|
+ // ZipOutputStream ࣺ ļ ļ е ѹ
|
|
|
+ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
|
|
|
+ for (int i = 0; i < srcfile.length; i++) {
|
|
|
+ FileInputStream in = new FileInputStream(srcfile[i]);
|
|
|
+ out.putNextEntry(new ZipEntry(srcfile[i].getName()));
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buf)) > 0) {
|
|
|
+ out.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ out.closeEntry();
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ //zou.log.Logger.getServerlog().error("ѹ ʧ "+e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void zipFiles(List<File> fileList, File zipfile) {
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ try {
|
|
|
+ // ZipOutputStream ࣺ ļ ļ е ѹ
|
|
|
+ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
|
|
|
+ for(File file : fileList) {
|
|
|
+ FileInputStream in = new FileInputStream(file);
|
|
|
+ out.putNextEntry(new ZipEntry(file.getName()));
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buf)) > 0) {
|
|
|
+ out.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ out.closeEntry();
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+
|
|
|
+ //zou.log.Logger.getServerlog().error("ѹ ʧ "+e.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static byte[] byteAryMap2Zip(Map<String,byte[]> byteAryMap) {
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ try {
|
|
|
+ // ZipOutputStream ࣺ ļ ļ е ѹ
|
|
|
+ ZipOutputStream out = new ZipOutputStream(baos);
|
|
|
+ for(String fileName : byteAryMap.keySet()) {
|
|
|
+ byte[] byteAry = byteAryMap.get(fileName);
|
|
|
+ ByteArrayInputStream in = new ByteArrayInputStream(byteAry);
|
|
|
+ out.putNextEntry(new ZipEntry(fileName));
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buf)) > 0) {
|
|
|
+ out.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ out.closeEntry();
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+
|
|
|
+ //zou.log.Logger.getServerlog().error("ѹ ʧ "+e.toString());
|
|
|
+ }
|
|
|
+ return baos.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean createFloder(File file) {
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdir();
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ public static boolean createFile(File file) {
|
|
|
+
|
|
|
+ if (!file.exists()) {
|
|
|
+ try {
|
|
|
+ file.createNewFile();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ public static String saveFile(String fileName,byte[] fileContent) {
|
|
|
+ File file1 = new File("D:\\ ļ \\flew\\");
|
|
|
+ File file = new File("D:\\ ļ \\flew\\"+fileName);
|
|
|
+ FileOutputStream out = null;
|
|
|
+ createFloder(file1);
|
|
|
+ createFile(file);
|
|
|
+ try {
|
|
|
+ out = new FileOutputStream(file);
|
|
|
+ out.write(fileContent);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return file.getPath();
|
|
|
+ }
|
|
|
+}
|