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

歡迎訪問 生活随笔!

生活随笔

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

java

java gzip 多个文件_Java Zip多文件压缩和 GZIP压缩

發布時間:2023/12/10 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java gzip 多个文件_Java Zip多文件压缩和 GZIP压缩 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**

* 多文件壓縮

*

* @author Administrator

*

*/

public class ZipCompress {

public static void main(String args[]) {

String[] filepaths = { "D:\\zip1.txt", "D:\\zip2.txt" };

try {

FileOutputStream f = new FileOutputStream("D://test.zip");

// 輸出校驗流,采用Adler32更快

CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());

//創建壓縮輸出流

ZipOutputStream zos = new ZipOutputStream(csum);

BufferedOutputStream out = new BufferedOutputStream(zos);

//設置Zip文件注釋

zos.setComment("A test of java Zipping");

for (String s : filepaths) {

System.out.println("Writing file " + s);

//針對單個文件建立讀取流

BufferedReader bin = new BufferedReader(new FileReader(s));

//ZipEntry ZIP 文件條目

//putNextEntry 寫入新條目,并定位到新條目開始處

zos.putNextEntry(new ZipEntry(s));

int c;

while ((c = bin.read()) != -1) {

out.write(c);

}

bin.close();

out.flush();

}

out.close();

FileInputStream fi = new FileInputStream("D://test.zip");

CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());

ZipInputStream in2 = new ZipInputStream(csumi);

BufferedInputStream bis = new BufferedInputStream(in2);

ZipEntry ze;

while ((ze = in2.getNextEntry()) != null) {

System.out.println("Reader File " + ze);

int x;

while ((x = bis.read()) != -1)

System.out.println(x);

}

//利用ZipFile解壓壓縮文件

ZipFile zf = new ZipFile("D:\\test.zip");

Enumeration e = zf.entries();

while(e.hasMoreElements()){

ZipEntry ze2 = (ZipEntry) e.nextElement();

System.out.println("File name : "+ze2);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 文件壓縮

* 把文件壓縮成GZIP 單一的流數據 并不是互異的數據

* GZIPOutputStream的使用

* @author Administrator

*

*/

public class GzipPcompress {

public static void main(String args[]) {

try {

BufferedReader in = new BufferedReader(new FileReader("D:\\gziptest.txt"));

BufferedOutputStream out = new BufferedOutputStream(

new GZIPOutputStream(new FileOutputStream("D:\\test.gz")));

System.out.println("Writing file");

int c;

while ((c = in.read()) != -1) {

out.write(c);

}

in.close();

out.close();

System.out.println("Reading file");

BufferedReader in2 = new BufferedReader(new InputStreamReader(

new GZIPInputStream(new FileInputStream("D:\\test.gz"))));

String s;

while((s=in2.readLine()) != null){

System.out.println(s);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

總結

以上是生活随笔為你收集整理的java gzip 多个文件_Java Zip多文件压缩和 GZIP压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

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