如何解決java.util.zip.ZipFile解壓后被java占用問(wèn)題
java.util.zip.ZipFile解壓后被java占用
在使用jdk自帶zip解壓工具解壓文件時(shí),調(diào)用ZipFile的getInputStream(ZipEntry entry)方法獲取實(shí)體輸入流后,正常關(guān)閉getInputStram返回的輸入流。
zip文件仍然被占用,導(dǎo)致java刪除zip文件失敗的問(wèn)題。
解決方法
在解壓完成后調(diào)用ZipFile的close()方法關(guān)閉所有已打開(kāi)的輸入流。
原因:根據(jù)源碼(jdk1.6)
若壓縮方式為STORED,則 getInputStream返回ZipFileInputStream類(lèi)的輸入流
該輸入流的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類(lèi)的輸入流
該輸入流的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類(lèi)提供的close()方法為:
主要區(qū)別應(yīng)該在于Store的壓縮方式,執(zhí)行了closeRequested = true 和close(zf),而 ZipFileInputStream只是調(diào)用了 freeEntry;
對(duì)于 壓縮方式為DEFLATED的情況,還未測(cè)試。
/**
* 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java代碼實(shí)現(xiàn)C盤(pán)文件統(tǒng)計(jì)工具
今天周末,給大家分享基于java代碼實(shí)現(xiàn)C盤(pán)文件統(tǒng)計(jì)工具,在這小編使用的版本是Maven-3.9.9,jdk1.8,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
利用SpringBoot實(shí)現(xiàn)RSA+AES混合加密
用戶(hù)注冊(cè)時(shí),密碼明文傳輸被攔截?支付信息在傳輸過(guò)程中被竊?。炕蛘呙舾袛?shù)據(jù)在接口調(diào)用時(shí)被中間人攻擊?再或者App被反編譯,接口參數(shù)被破解?今天我們就來(lái)聊聊如何用SpringBoot實(shí)現(xiàn)RSA+AES混合加密,讓你的接口數(shù)據(jù)傳輸更安全,需要的朋友可以參考下2026-01-01
knife4j+springboot3.4異常無(wú)法正確展示文檔
本文主要介紹了knife4j+springboot3.4異常無(wú)法正確展示文檔,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Java實(shí)現(xiàn)雪花算法(snowflake)
這篇文章主要介紹了Java實(shí)現(xiàn)雪花算法(snowflake),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Redis Java Lettuce驅(qū)動(dòng)框架原理解析
這篇文章主要介紹了Redis Java Lettuce驅(qū)動(dòng)框架原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼
這篇文章主要介紹了線程池ThreadPoolExecutor并行處理實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

