国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java壓縮文件ZIP實(shí)例代碼

2019-11-26 15:49:49
字體:
供稿:網(wǎng)友

提示:java.util.zipoutputstream

        java API壓縮為zip文件

代碼:

復(fù)制代碼 代碼如下:

package com.gaoqi.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 壓縮文件
* @author Administrator
*
*/
public class SimpleZip {

public static final int BUFFER_SIZE = 1024;

public static void main(String[] args) throws IOException {
String src = "d://chat";
String des = "d://chat01.zip";
ZipOutputStream zos = null;
try{
zos = new ZipOutputStream(new FileOutputStream(des));
File srcFile = new File(src);
String base = srcFile.getName();
fileZip(srcFile,zos,base);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(zos!=null){
zos.close();
}
}
System.out.println("文件壓縮成功啦" + src);
}

private static void fileZip(File srcFile, ZipOutputStream zos, String base)
throws Exception{
// TODO Auto-generated method stub
if(!srcFile.exists()){
System.out.println("文件不存在" + srcFile.getPath());
}
if(srcFile.isFile()){
zos.putNextEntry(new ZipEntry(base));
FileInputStream fis = new FileInputStream(srcFile);
byte[] buf = new byte[BUFFER_SIZE];
int n=0;
while((n=fis.read(buf, 0, buf.length))!=-1){
zos.write(buf, 0, n);
}
fis.close();
}else{
if(srcFile.isDirectory()) {
base = base + File.separator;
File[] subFiles = srcFile.listFiles();
for (File subFile : subFiles) {
fileZip(subFile, zos, base + subFile.getName());
}
}
}
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万山特区| 平原县| 石景山区| 民乐县| 金塔县| 河津市| 兴文县| 阿拉善左旗| 阳高县| 潍坊市| 信宜市| 思茅市| 民和| 聊城市| 青冈县| 垦利县| 岑溪市| 滁州市| 内黄县| 专栏| 临武县| 女性| 阜新市| 平昌县| 沈阳市| 太白县| 襄垣县| 遵化市| 西乌珠穆沁旗| 东丰县| 博野县| 安阳县| 宜黄县| 胶州市| 宜城市| 鄄城县| 苍梧县| 麻江县| 廊坊市| 石城县| 富源县|