package com.xiaoniu.subsystem.strategy;

import cn.hutool.core.io.IoUtil;
import com.xiaoniu.subsystem.strategy.bean.FileObj;
import com.xiaoniu.subsystem.strategy.callback.RarExtractCallback;
import lombok.extern.slf4j.Slf4j;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.util.ByteArrayStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Slf4j
public class RarAnalysisStrategy implements UnZipStrategy {
    @Override
    public List<FileObj> analysis(InputStream inputStream) {
        try {
            byte[] bytes = IoUtil.readBytes(inputStream);
            ByteArrayStream byteArrayStream = new ByteArrayStream(bytes, false);
            IInArchive inArchive = SevenZip.openInArchive(null, byteArrayStream);
            int[] in = new int[inArchive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            //使用回调函数
            RarExtractCallback rarExtractCallback = new RarExtractCallback(inArchive);
            inArchive.extract(in, false, rarExtractCallback);
            byteArrayStream.close();
            inputStream.close();
            return rarExtractCallback.getFileObjs();
        } catch (IOException e) {
            log.error("rar格式的压缩文件在解压时发生错误：",e);
            throw new RuntimeException(e);
        }


    }



}
