最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)多層級zip解壓的示例代碼

 更新時間:2024年12月23日 11:09:30   作者:鐵流是寶寶  
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)多層級zip解壓的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考一下

前言

項目中偶然需要,希望能處理嵌套的壓縮包,但是又不希望把文件解壓處理。原本不希望重復(fù)造輪子,但沒有發(fā)現(xiàn)很好用的現(xiàn)成案例,就簡單處理了一下。

正文

java做zip解壓一般使用 ZipFile? 或者 ZipInputStream?。

在實際使用中,遇到了zip清單屬性無法讀取的報錯,最終采用了apache的ZipArchiveInputStream。主要是allowStoredEntriesWithDataDescriptor?屬性。

代碼完整使用的依賴如下:

	    <dependency>
	      <groupId>org.apache.commons</groupId>
	      <artifactId>commons-compress</artifactId>
	      <version>1.19</version>
	    </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.26</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>

代碼主要為符合業(yè)務(wù)需求而寫,比較簡陋。支持單次解壓和遞歸解壓,均通過回調(diào)返回緩沖流(無法關(guān)閉的緩沖流)。

必須要注意的是,一定不能提前關(guān)閉ZipArchiveInputStream,這個流一次會在getNextZipEntry后再次填充。

回調(diào)如果采用字節(jié)對內(nèi)存的壓力可能會比較大,所以通過緩沖流返回數(shù)據(jù)。為防止多人協(xié)作中出現(xiàn)誤關(guān)閉流,使用不關(guān)閉源流的緩沖流工具。

如果有需要解壓指定包,在入?yún)⒓右粋€filter就可以實現(xiàn)。

完整代碼實例

package xxx;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
用于輔助的不關(guān)閉原流的緩沖流
*/
public class NoCloseBufferStream extends BufferedInputStream {
public NoCloseBufferStream(InputStream in) {
super(in);
}
@Override
public void close() throws IOException {
//不實現(xiàn)任何東西就不會關(guān)閉原流
}
}
package xxx; //your package
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
 
/**
 * 注意:初始的輸入流是不會主動關(guān)閉的
 *
 * @author 鐵流
 */
@Slf4j
public class UnZipUtil {
 
    public static void main(String[] args) throws IOException {
 
        try (InputStream inputStream = Files.newInputStream(new File("/Users/tieliu/Desktop/test/aaaa.zip").toPath());) {
            loopUnzip(inputStream, (level, path, basePath, is) -> {
                is.close();
                log.info(" level: {},path: {},basePath: {}", level, path, basePath);
                return true;
            });
        }
    }
 
    /**
     * 遞歸解壓zip,只能解壓zip后綴名的壓縮文件
     *
     * @param inputStream  初始文件輸入流
     * @param loopCallBack 遞歸回調(diào),返回值控制是否向下遞歸
     * @throws IOException 文件流異常
     */
    public static void loopUnzip(InputStream inputStream, LoopCallBack loopCallBack) throws IOException {
        loopUnzip(inputStream, 0, "", loopCallBack);
    }
 
    private static void loopUnzip(InputStream inputStream, int level, String basePath, LoopCallBack loopCallBack) throws IOException {
        decompress(inputStream, (path, is) -> {
            // 此處決定是否繼續(xù)向下
            if (loopCallBack.call(level, path, basePath, is) && path.endsWith(".zip")) {
                loopUnzip(is, level + 1, basePath + "/" + path, loopCallBack);
            }
        });
    }
 
    /**
     * 解壓zip,必須是zip結(jié)尾的文件(錯誤屬性的文件會被排除,因為不排除java也解壓不了)
     *
     * @param inputStream 初始輸入流
     * @param callBack    回調(diào)
     * @throws IOException io異常
     */
    public static void decompress(InputStream inputStream, CallBack callBack) throws IOException {
        try (NoCloseBufferStream bufferedInputStream = new NoCloseBufferStream(inputStream);
             ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(bufferedInputStream, CharsetUtil.defaultCharset().name(), true, true)) {
            decompress(zipInputStream, callBack);
        }
    }
 
    public static void decompress(byte[] bytes, CallBack callBack) throws IOException {
        try (ByteArrayInputStream inputStream = IoUtil.toStream(bytes);) {
            bytes = null;
            decompress(inputStream, callBack);
        }
    }
 
    private static void decompress(ZipArchiveInputStream inputStream, CallBack callBack) throws IOException {
        ZipArchiveEntry nextEntry = inputStream.getNextZipEntry();
        while (nextEntry != null) {
            final String name = nextEntry.getName();
            //過濾無用文件
            if (!name.startsWith("__MACOSX") && !name.contains(".DS_Store") && !name.contains("Thumbs.db") && !name.startsWith("._")) {
                if (!nextEntry.isDirectory()) {
                    callBack.call(name, new NoCloseBufferStream(inputStream));
                }
            }
            nextEntry = inputStream.getNextZipEntry();
        }
    }
 
    @FunctionalInterface
    public static interface CallBack {
        void call(String relativePath, InputStream is) throws IOException;
    }
 
    @FunctionalInterface
    public static interface LoopCallBack {
        boolean call(int level, String relativePath, String basePath, InputStream is) throws IOException;
    }
 
}

到此這篇關(guān)于java實現(xiàn)多層級zip解壓的示例代碼的文章就介紹到這了,更多相關(guān)java zip解壓內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

寿阳县| 吴川市| 正宁县| 昆山市| 辰溪县| 恭城| 沂水县| 准格尔旗| 德保县| 巍山| 双鸭山市| 鄱阳县| 德保县| 溆浦县| 杭州市| 阿坝县| 白朗县| 信宜市| 乐东| 工布江达县| 霍邱县| 融水| 外汇| 浦县| 安吉县| 乌兰浩特市| 甘南县| 天水市| 凤庆县| 惠来县| 济源市| 清水县| 哈密市| 大同县| 伽师县| 岳西县| 顺昌县| 茂名市| 锡林浩特市| 织金县| 新安县|