日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java putnextentry_Java对zip格式压缩和解压缩

發布時間:2024/10/6 java 207 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java putnextentry_Java对zip格式压缩和解压缩 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java對zip格式壓縮和解壓縮

通過使用java的相關類可以實現對文件或文件夾的壓縮,以及對壓縮文件的解壓。

1.1 ZIP和GZIP的區別

gzip是一種文件壓縮工具(或該壓縮工具產生的壓縮文件格式),它的設計目標是處理單個的文件。gzip在壓縮文件中的數據時使用的就是zlib。為了保存與文件屬性有關的信息,gzip需要在壓縮文件(*.gz)中保存更多的頭信息內容,而zlib不用考慮這一點。但gzip只適用于單個文件,所以我們在UNIX/Linux上經常看到的壓縮包后綴都是*.tar.gz或*.tgz,也就是先用tar把多個文件打包成單個文件,再用gzip壓縮的結果。

zip只是一種數據結構,跟rar同類型。zip是適用于壓縮多個文件的格式(相應的工具有PkZip和WinZip等),因此,zip文件還要進一步包含文件目錄結構的信息,比gzip的頭信息更多。但需要注意,zip格式可采用多種壓縮算法,我們常見的zip文件大多不是用zlib的算法壓縮的,其壓縮數據的格式與gzip大不一樣。

1.2相關類與接口:

Checksum接口:表示數據校驗和的接口,被類Adler32和CRC32實現

Adler32?:使用Alder32算法來計算Checksum數目

CRC32:使用CRC32算法來計算Checksum數目

CheckedInputStream:InputStream派生類,可得到輸入流的校驗和Checksum,用于校驗數據的完整性

CheckedOutputStream?:OutputStream派生類,可得到輸出流的校驗Checksum,?用于校驗數據的完整性

DeflaterOutputStream?:壓縮類的基類。

ZipOutputStream:DeflaterOutputStream的一個子類,把數據壓縮成Zip文件格式

GZIPOutputStream?:DeflaterOutputStream的一個子類,把數據壓縮成GZip文件格式

InflaterInputStream:解壓縮類的基類

ZipInputStream:InflaterInputStream的一個子類,能解壓縮Zip格式的數據

GZIPInputStream:InflaterInputStream的一個子類,能解壓縮Zip格式的數據

ZipEntry?類:表示 ZIP 文件條目

ZipFile?類:此類用于從 ZIP 文件讀取條目

1.3 壓縮文件

下面實例我們使用了apache的zip工具包(所在包為ant.jar ),因為java類型自帶的不支持中文路徑,不過兩者使用的方式是一樣的,只是apache壓縮工具多了設置編碼方式的接口,其他基本上是一樣的。另外,如果使用org.apache.tools.zip.ZipOutputStream來壓縮的話,我們只能使用org.apache.tools.zip.ZipEntry來解壓,而不能使用java.util.zip.ZipInputStream來解壓讀取了,當然apache并未提供ZipInputStream類。

publicstaticvoidcompress(String srcFilePath, String destFilePath) {

File src= newFile(srcFilePath);

if(!src.exists()) {

thrownewRuntimeException(srcFilePath+ "不存在");

}

File zipFile= newFile(destFilePath);

try{

FileOutputStream fos = new FileOutputStream(zipFile);

CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());

ZipOutputStream zos = new ZipOutputStream(cos);

String baseDir = "";

compressbyType(src, zos, baseDir);

zos.close();

} catch(Exception e) {

// TODOAuto-generated catch block

e.printStackTrace();

}

}

privatestaticvoidcompressbyType(File src, ZipOutputStream zos,

String baseDir) {

if(!src.exists())

return;

System.out.println("壓縮"+ baseDir+ src.getName());

if(src.isFile()) {

compressFile(src, zos, baseDir);

} elseif(src.isDirectory()) {

compressDir(src, zos, baseDir);

}

}

/**

*壓縮文件

*

*/

privatestaticvoidcompressFile(File file, ZipOutputStream zos,

String baseDir) {

if(!file.exists())

return;

try{

BufferedInputStream bis = new BufferedInputStream(

new FileInputStream(file));

ZipEntry entry = new ZipEntry(baseDir + file.getName());

zos.putNextEntry(entry);

int count;

byte[] buf = new byte[BUFSIZE];

while ((count = bis.read(buf)) != -1) {

zos.write(buf, 0, count);

}

bis.close();

} catch(Exception e) {

// TODO: handle exception

}

}

/**

*壓縮文件夾

*

*/

privatestaticvoidcompressDir(File dir, ZipOutputStream zos,

String baseDir) {

if(!dir.exists())

return;

File[] files= dir.listFiles();

if(files.length == 0){

try {

zos.putNextEntry(new ZipEntry(baseDir + dir.getName()

+ File.separator));

} catch (IOException e) {

e.printStackTrace();

}

}

for(File file: files) {

compressbyType(file, zos, baseDir+ dir.getName() + File.separator);

}

}

總結步驟:

創建壓縮到的文件File zipFile= newFile(destFilePath);

根據zipFile生成ZipOutputStream用于寫入即將被壓縮的文件

FileOutputStream fos = new FileOutputStream(zipFile);

CheckedOutputStream cos = new ???? ????????????????????????????????CheckedOutputStream(fos, new CRC32());

ZipOutputStream zos = new ZipOutputStream(cos);

循環遍歷源文件,首先需要創建ZipEntry用于標記壓縮文件中含有的條目

ZipEntry entry = new ZipEntry(baseDir + file.getName());

然后將條目增加到ZipOutputStream中,

zos.putNextEntry(entry);

最后再調用要寫入條目對應文件的輸入流讀取文件內容寫入到壓縮文件中。

BufferedInputStream bis = new BufferedInputStream(

new FileInputStream(file));

ZipEntry entry = new ZipEntry(baseDir + file.getName());

zos.putNextEntry(entry);

int count;

byte[] buf = new byte[BUFSIZE];

while ((count = bis.read(buf)) != -1) {

zos.write(buf, 0, count);

}

注意:如果是空目錄直接zos.putNextEntry(new ZipEntry(baseDir + dir.getName()+ File.separator))并不用寫入文件內容,其中最主要的涉及到目錄的壓縮的,就是這一句話?out.putNextEntry(new?ZipEntry(base?+?"/"));?//放入一級目錄 (防止空目錄被丟棄)

1.4解壓zip文件

publicstaticvoiddecompress(String srcPath, String dest) throwsException {

File file= newFile(srcPath);

if(!file.exists()) {

thrownewRuntimeException(srcPath+ "所指文件不存在");

}

ZipFile zf= newZipFile(file);

Enumeration

entries= zf.getEntries();

ZipEntry

entry= null;

while(entries.hasMoreElements()) {

entry= (ZipEntry) entries.nextElement();

System.out.println("解壓"+ entry.getName());

if(entry.isDirectory()) {

String dirPath= dest+ File.separator+ entry.getName();

File dir= newFile(dirPath);

dir.mkdirs();

} else{

//表示文件

File f= newFile(dest+ File.separator+ entry.getName());

if(!f.exists()) {

String dirs= FileUtils.getParentPath(f);

File parentDir= newFile(dirs);

parentDir.mkdirs();

}

f.createNewFile();

//將壓縮文件內容寫入到這個文件中

InputStream is= zf.getInputStream(entry);

FileOutputStream fos= newFileOutputStream(f);

intcount;

byte[] buf= newbyte[8192];

while((count= is.read(buf)) != -1) {

fos.write(buf, 0, count);

}

is.close();

fos.close();

}

}

}

總結

以上是生活随笔為你收集整理的java putnextentry_Java对zip格式压缩和解压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。