| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package com.xjrsoft.common.utils;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.List;
- import java.util.Map;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- 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();
- }
- /**
- * 将MultipartFile转成ZipFile
- * @param multipartFile
- * @return
- * @throws IOException
- */
- public static ZipFile convertToZipFile(MultipartFile multipartFile) throws IOException {
- // 创建临时文件
- String originalFilename = multipartFile.getOriginalFilename();
- Path tempPath = Files.createTempFile("temp", originalFilename.substring(originalFilename.lastIndexOf('.')));
- multipartFile.transferTo(tempPath); // 将MultipartFile内容写入临时文件
- // 使用ZipFile读取临时文件
- ZipFile zipfile = new ZipFile(tempPath.toFile(), Charset.forName("GBK"));
- return zipfile;
- }
- }
|