java读取打包时间_java 打包的两种方式

import java.io.*;importjava.util.ArrayList;importjava.util.List;importjava.util.zip.ZipEntry;importjava.util.zip.ZipOutputStream;public classCreateZip {//将所有的文件打包到一个zip包中 没有目录结构 且运用的是jdk自带的打包工具

public static voidmain(String[] args) {//要打包的文件路径

List listKey = new ArrayList();

listKey.add("D:\\img\\1.png");

listKey.add("D:\\img\\2.png");

listKey.add("D:\\img\\论文解析详细设计.docx");

File zipFile= new File("D:\\zipTest\\img.zip");//最终打包的压缩包

System.out.println("zipFile exists: " +zipFile.exists());

System.out.println("zipFile size: " +zipFile.length());

packageZip(zipFile,listKey);

System.out.println("zipFile exists2: " +zipFile.exists());

System.out.println("zipFile size: " +zipFile.length());

}public static boolean packageZip(File zipFile, ListlistKey){//图片打包操作

ZipOutputStream zipStream = null;

FileInputStream zipSource= null;

BufferedInputStream bufferStream= null;try{

zipStream= new ZipOutputStream(new FileOutputStream(zipFile));//用这个构造最终压缩包的输出流

for(String picKey : listKey) {

File file= newFile(picKey);

zipSource= newFileInputStream(file);byte[] bufferArea = new byte[1024 * 10];//读写缓冲区//压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样

ZipEntry zipEntry = newZipEntry(file.getName());

zipStream.putNextEntry(zipEntry);//定位到该压缩条目位置,开始写入文件到压缩包中

bufferStream = new BufferedInputStream(zipSource, 1024 * 10);//输入缓冲流

int read = 0;//在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1]//的元素都不会受到影响。这个是官方API给出的read方法说明,经典!

while ((read = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {

zipStream.write(bufferArea,0, read);

}

}

}catch(Exception e) {return false;

}finally{//关闭流

try{if (null !=bufferStream)

bufferStream.close();if (null !=zipStream)

zipStream.close();if (null !=zipSource)

zipSource.close();

}catch(IOException e) {

e.printStackTrace();return false;

}

}return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值