多线程解压缩包

package com.my.tpp.utils;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.extern.slf4j.Slf4j;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


/**
 * @ClassName PicturesVo
 * @Description TODO
 * @Author fan
 * @Date 2023/5/22 9:33
 * @Version 1.0
 */
@Slf4j
@Service
public class 你的Util {

    private static final String format = "yyyy/MM/dd HH:mm:ss";
    private static SimpleDateFormat sdf = new SimpleDateFormat(format);

    

    public boolean checkWord(String word) {
    
        return !StringUtil.isEmpty(word);
    }


    /**
     * @Description:获取excel表中的图片
     * @throws IOException
     * @throws EncryptedDocumentException
     * @param file 文件输入流
     * @param sheetNum  Excel表中的sheet编号
     * @return: java.util.Map
     * @Author: fan
     * @Date: 2023/5/22 11:05
     */
    public static Map getPictureFromExcel(File file, int sheetNum) throws EncryptedDocumentException, IOException {
        //获取图片PictureData集合
        String fileName = file.getName();
        Workbook workbook = null;
        FileInputStream fileInputStream = null;
        try {
            if (StringUtil.isEmpty(fileName)) {
                return null;
            }
            fileInputStream = new FileInputStream(file);
            if (fileName.endsWith("xls")) {
                //2003
                workbook = new HSSFWorkbook(fileInputStream);
                HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(sheetNum - 1);
                Map pictures = getPictures(sheet);
                return pictures;
            } else if (fileName.endsWith("xlsx")) {
                //2007
                workbook = new XSSFWorkbook(fileInputStream);
                XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetNum - 1);
                Map pictures = getPictures(sheet);
                return pictures;
            }
        } catch (Exception e) {
            log.error("[getPictureFromExcel()-Exception]  Exception状态异常 --->:{} , 栈信息:{}" , e.getMessage() , getExceptionStr(e));
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (Exception ee) {
                log.error("[getPictureFromExcel()-Exception]  Exception状态异常2 --->:{} , " , ee.getMessage() );
            }
        }
        return new HashMap();
    }

    /**
     * @Description:获取excel表中的图片
     * @throws IOException
     * @throws EncryptedDocumentException
     * @param multipartFile 文件输入流
     * @param sheetNum  Excel表中的sheet编号
     * @return: java.util.Map
     * @Author: fan
     * @Date: 2023/5/22 11:05
     */
    public static Map getPictureFromExcel(MultipartFile multipartFile, int sheetNum) {
        //获取图片PictureData集合
        Workbook workbook = null;
        InputStream inputStream = null;
        try {
            if (multipartFile != null) {
                inputStream = multipartFile.getInputStream();
                workbook = new XSSFWorkbook(inputStream);
                XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetNum - 1);
                Map pictures = getPictures(sheet);
                return pictures;
            }
        } catch (Exception e) {
            log.error("[getAlertInfoList()-Exception]  Exception状态异常 --->:{}" , e.getMessage() );
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e1) {
                log.error("[getAlertInfoList()-Exception]  Exception状态异常1 --->:{} , " , e1.getMessage() );
            }
        }
        return new HashMap();
    }

    /**
     * 获取图片和位置 (xls版)
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map getPictures (HSSFSheet sheet)  {
        Map map = new HashMap();
        List list = sheet.getDrawingPatriarch().getChildren();
        for (HSSFShape shape : list) {
            if (shape instanceof HSSFPicture) {
                HSSFPicture picture = (HSSFPicture) shape;
                HSSFClientAnchor cAnchor = picture.getClientAnchor();
                PictureData pdata = picture.getPictureData();
                String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行号-列号
                map.put(key, pdata);
            }
        }
        return map;
    }

    /**
     * 获取图片和位置 (xlsx版)
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map getPictures (XSSFSheet sheet)  {
        Map map = new HashMap();
        List list = sheet.getRelations();
        for (POIXMLDocumentPart part : list) {
            if (part instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) part;
                List shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    /*XSSFPicture picture = (XSSFPicture) shape;
                    XSSFClientAnchor anchor = picture.getPreferredSize();
                    CTMarker marker = anchor.getFrom();
                    String key = marker.getRow() + "-" + marker.getCol();
                    map.put(key, picture.getPictureData());*/
                    XSSFPicture picture = (XSSFPicture) shape;
                    //解决图片空指针报错问题 fan  2023-05-30
                    XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
//					XSSFClientAnchor anchor = pic.getPreferredSize();
                    CTMarker ctMarker = anchor.getFrom();
                    //解决用户头像格式错误   fan  2023-05-30
                    String picIndex = ctMarker.getRow() + "-" + anchor.getCol1();
                    map.put(picIndex, picture.getPictureData());
                }
            }
        }
        return map;
    }

    public static String doZero(String value) {
        String val = value;
        if (value.indexOf(".") != -1) {
            val = value.replaceAll("0+?$", "");
            val = val.replaceAll("[.]$", "");
        }
        return val;
    }



    /**
     * @desc:获取单元格内容
     * @param cell
     * @return
     *         异常时返回null
     */
    public static String getCellValueText(Cell cell) {
        try {
            String text = "";
            if (cell == null) {
                return text;
            } else {
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC://0
                        if (DateUtil.isCellDateFormatted(cell)) {// 判断是否为日期类型
                            Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
                            return DateUtils.covertDate2String(date);//这里做个时间转换就补贴了
                        }
                        Double value = cell.getNumericCellValue();
                        BigDecimal bd = new BigDecimal(value.toString());
                        text = bd.toPlainString();
                        text = doZero(text);
                        break;
                    case Cell.CELL_TYPE_STRING://1
                        text = cell.getStringCellValue().trim();
                    case Cell.CELL_TYPE_FORMULA://2
                        try {
                            text = String.valueOf(cell.getNumericCellValue());
                        } catch (IllegalStateException e) {
                            text = String.valueOf(cell.getRichStringCellValue());
                        }
                        break;
                }
                return text;
            }
        } catch (Exception e) {
            log.error("[getCellValueText()-Exception]  Exception状态异常 --->:{} " , e.getMessage() );
            return null;
        }
    }

    /**
     * @Description:获取excel里面的内容
     * @param multipartFile
     * @return: java.util.List>
     * @Author: fan
     * @Date: 2023/6/14 11:02
     */
    public static List> getPictureList(MultipartFile multipartFile){
        List> list = new ArrayList<>();
        try {
            //获取图片数据
            Map map = getPictureFromExcel(multipartFile, 1);
            InputStream inputStream = multipartFile.getInputStream();
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            int lastRow = sheet.getLastRowNum();
            int lastCell = sheet.getRow(0).getLastCellNum();
            for (int i = 1; i <= lastRow; i++) {
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }
                Map param = new HashMap<>();
                for (int j = 0; j < lastCell; j++) {
                    Cell cell = row.getCell(j);
                    String value = getCellValueText(cell);
     

你可能感兴趣的:(SpringBoot,java)