首页 / JAVA / java 打包的两种方式
java 打包的两种方式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java 打包的两种方式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4231字,纯文字阅读大概需要7分钟。
内容图文
一.利用java 自带的util.zip 将多个文件打包在一个文件夹之中
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class CreateZip { //将所有的文件打包到一个zip包中 没有目录结构 且运用的是jdk自带的打包工具 public static void main(String[] args) { //要打包的文件路径 List<String> listKey = new ArrayList<String>(); 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, List<String> listKey){ //图片打包操作 ZipOutputStream zipStream = null; FileInputStream zipSource = null; BufferedInputStream bufferStream = null; try { zipStream = new ZipOutputStream(new FileOutputStream(zipFile));// 用这个构造最终压缩包的输出流 for (String picKey : listKey) { File file = new File(picKey); zipSource = new FileInputStream(file); byte[] bufferArea = new byte[1024 * 10];// 读写缓冲区 // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样 ZipEntry zipEntry = new ZipEntry(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; } }
二,采用apache的ant打包,包含文件夹一起打包
import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class CreateZipAnt { public static void main(String[] args) { try { long starTime = 0; long endTime = 0; System.out.println("开始压缩..."); starTime = System.currentTimeMillis(); ZIP("D:\\img", "D:\\zipTest\\img.zip"); endTime = System.currentTimeMillis(); System.out.println("压缩完毕!花费时间: " + (endTime - starTime) + " 毫秒!"); } catch (Exception ex) { ex.printStackTrace(); } } public static void ZIPDIR(String sourceDir, ZipOutputStream zos, String tager) throws IOException { //System.out.println(tager); ZipEntry ze = new ZipEntry(tager); zos.putNextEntry(ze); // 提取要压缩的文件夹中的所有文件 File f = new File(sourceDir); File[] flist = f.listFiles(); if (flist != null) { // 如果该文件夹下有文件则提取所有的文件进行压缩 for (File fsub : flist) { if (fsub.isDirectory()) { //如果是目录则进行目录压缩 ZIPDIR(fsub.getPath(), zos, tager + fsub.getName() + "/"); } else { //如果是文件,则进行文件压缩 ZIPFile(fsub.getPath(), zos, tager + fsub.getName()); } } } } public static void ZIP(String source, String zipFileName) throws IOException { ZipOutputStream zos = new ZipOutputStream(new File(zipFileName)); // 设置压缩的时候文件名编码为gb2312 zos.setEncoding("gb2312"); File f = new File(source); if (f.isDirectory()) { // 如果直接压缩文件夹 ZIPDIR(source, zos, f.getName() + "/");// 此处使用/来表示目录,如果使用\\来表示目录的话,会导致压缩后的文件目录组织形式在解压缩的时候不能正确识别。 } else { // 如果直接压缩文件 ZIPDIR(f.getPath(), zos, new File(f.getParent()).getName() + "/"); ZIPFile(f.getPath(), zos, new File(f.getParent()).getName() + "/" + f.getName()); } zos.closeEntry(); zos.close(); } public static void ZIPFile(String sourceFileName, ZipOutputStream zos, String tager) throws IOException { ZipEntry ze = new ZipEntry(tager); zos.putNextEntry(ze); // 读取要压缩文件并将其添加到压缩文件中 FileInputStream fis = new FileInputStream(new File(sourceFileName)); byte[] bf = new byte[2048]; int location = 0; while ((location = fis.read(bf)) != -1) { zos.write(bf, 0, location); } fis.close(); } }
内容总结
以上是互联网集市为您收集整理的java 打包的两种方式全部内容,希望文章能够帮你解决java 打包的两种方式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】