zip壓縮檔案(二)

2022-06-11 22:48:12 字數 2194 閱讀 5780

普通壓縮檔案以20m大小的檔案為例

public

static

void

main(string args)

public

static

void

zipfilenobuffer(string zipfilepath, string sourcefilepath)

input.close();

zipout.closeentry();

zipout.close();

//時間耗時

system.out.println("壓縮完畢,耗時:[" + printtimeconsuming(begintime) + "] ms ...");

} catch

(exception e)

}

結果:

connected to the target vm, address: '127.0.0.1:49281', transport: 'socket'

開始壓縮檔案、檔案大小:[21.24m] ...

壓縮完畢,耗時:[68652] ms ...

disconnected from the target vm, address: '127.0.0.1:49281', transport: 'socket'

利用緩衝區bufferinputstream 優化

1

private

static

void

firstzipfile(string zipfilepath, string sourcefilepath)

18bufferedinputstream.close();

19input.close();

20bufferedoutputstream.close();

21zipout.close();

22fileout.close();

23//

時間耗時

24 system.out.println("壓縮完畢,耗時:[" + printtimeconsuming(begintime) + "] ms ...");

25 } catch

(exception e)

28 }

結果:耗時縮短了(68652-2501)毫秒

connected to the target vm, address: '127.0.0.1:51524', transport: 'socket'

開始壓縮檔案、檔案大小:[21.24m] ...

壓縮完畢,耗時:[2501] ms ...

disconnected from the target vm, address: '127.0.0.1:51524', transport: 'socket'

說明:這是乙個呼叫本地方法與原生作業系統進行互動,從磁碟中讀取資料。每讀取乙個位元組的資料就呼叫一次本地方法與作業系統互動,是非常耗時的。例如我們現在有30000個位元組的資料,如果使用fileinputstream那麼就需要呼叫30000次的本地方法來獲取這些資料,而如果使用緩衝區的話(這裡假設初始的緩衝區大小足夠放下30000位元組的資料)那麼只需要呼叫一次就行。因為緩衝區在第一次呼叫read()方法的時候會直接從磁碟中將資料直接讀取到記憶體中。隨後再乙個位元組乙個位元組的慢慢返回。

第二次優化:

1

/**2

* 第二次優化使用 channel3*/

4private

static

void

secondzipfile(string zipfilepath, string sourcefilepath ) catch

(exception e)

28 }

結果:比第一次優化耗時縮短了(2501-1810)毫秒

connected to the target vm, address: '127.0.0.1:52021', transport: 'socket'

開始壓縮檔案、檔案大小:[21.24m] ...

壓縮完畢,耗時:[1810] ms ...

disconnected from the target vm, address: '127.0.0.1:52021', transport: 'socket'

Python讀寫zip壓縮檔案

python自帶模組zipfile可以完成zip壓縮檔案的讀寫,而且使用非常方便,下面我們就來演示一下python讀寫zip檔案。下面的 給出了用python讀取zip檔案,列印出壓縮檔案裡面所有的檔案,並讀取壓縮檔案中的第乙個檔案。import zipfile z zipfile zipfile ...

go語言zip壓縮檔案

博海無涯。上 func zipfiles filename string,files string,oldform,newform string error defer newzipfile.close zipwriter zip.newwriter newzipfile defer zipwrit...

Zip壓縮檔案工具類

反序列化zip檔案 param path return throws ioexception public static mapbyte deserial string path throws ioexception map.put filename,baos.tobytearray zipinpu...