如何解決java.util.zip.ZipFile解壓后被java占用問題
java.util.zip.ZipFile解壓后被java占用
在使用jdk自帶zip解壓工具解壓文件時,調(diào)用ZipFile的getInputStream(ZipEntry entry)方法獲取實體輸入流后,正常關(guān)閉getInputStram返回的輸入流。
zip文件仍然被占用,導(dǎo)致java刪除zip文件失敗的問題。
解決方法
在解壓完成后調(diào)用ZipFile的close()方法關(guān)閉所有已打開的輸入流。
原因:根據(jù)源碼(jdk1.6)
若壓縮方式為STORED,則 getInputStream返回ZipFileInputStream類的輸入流
該輸入流的close()方法如下:
public void close() { rem = 0; synchronized (ZipFile.this) { if (jzentry != 0 && ZipFile.this.jzfile != 0) { freeEntry(ZipFile.this.jzfile, jzentry); jzentry = 0; } } } // freeEntry releases the C jzentry struct. private static native void freeEntry(long jzfile, long jzentry);
若壓縮方式為DEFLATED,則 getInputStream返回InflaterInputStream類的輸入流
該輸入流的close()方法如下:
protected Inflater inf; /** * Closes this input stream and releases any system resources associated * with the stream. * @exception IOException if an I/O error has occurred */ public void close() throws IOException { if (!closed) { if (usesDefaultInflater) inf.end(); in.close(); closed = true; } } public void end() { synchronized (zsRef) { long addr = zsRef.address(); zsRef.clear(); if (addr != 0) { end(addr); buf = null; } } } public class Inflater { private native static void end(long addr); }
而ZipFile類提供的close()方法為:
主要區(qū)別應(yīng)該在于Store的壓縮方式,執(zhí)行了closeRequested = true 和close(zf),而 ZipFileInputStream只是調(diào)用了 freeEntry;
對于 壓縮方式為DEFLATED的情況,還未測試。
/** * Closes the ZIP file. * <p> Closing this ZIP file will close all of the input streams * previously returned by invocations of the {@link #getInputStream * getInputStream} method. * * @throws IOException if an I/O error has occurred */ public void close() throws IOException { synchronized (this) { closeRequested = true; if (jzfile != 0) { // Close the zip file long zf = this.jzfile; jzfile = 0; close(zf); // Release inflaters synchronized (inflaters) { int size = inflaters.size(); for (int i = 0; i < size; i++) { Inflater inf = (Inflater)inflaters.get(i); inf.end(); } } } } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于springboot設(shè)置Https請求過程解析
這篇文章主要介紹了基于springboot設(shè)置Https請求過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Spring Cloud中使用Eureka的詳細(xì)過程
Eureka 是 Netflix 開源的一個服務(wù)發(fā)現(xiàn)組件,它在微服務(wù)架構(gòu)中扮演著重要的角色,這篇文章主要介紹了Spring Cloud中如何使用Eureka,需要的朋友可以參考下2024-07-07詳解SpringMVC的攔截器鏈實現(xiàn)及攔截器鏈配置
攔截器(Interceptor)是一種動態(tài)攔截方法調(diào)用的機制,在SpringMVC中動態(tài)攔截控制器方法的執(zhí)行。本文將詳細(xì)講講SpringMVC中攔截器參數(shù)及攔截器鏈配置,感興趣的可以嘗試一下2022-08-08關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解
這篇文章主要介紹了關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解,Spring中 @Transactional 注解,默認(rèn)情況下,只對拋出的RuntimeException 異常,才會事務(wù)回滾,需要的朋友可以參考下2023-05-05