小编给大家分享一下java如何实现多个文件压缩成压缩包,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
具体内容如下
需要用到的ant.jar
package util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class ZipTools { public static final String ZIP_FILENAME = ""; //需要解压缩的文件名 public static final String ZIP_DIR = ""; //需要压缩的文件夹 public static final String UN_ZIP_DIR = ""; //要解压的文件目录 public static final int BUFFER = 1024 ; //缓存大小 public static void zipFile(String baseDir,String fileName) throws Exception{ List fileList=getSubFiles(new File(baseDir)); ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(fileName)); ZipEntry ze=null; byte[] buf=new byte[BUFFER]; int readLen=0; for(int i = 0; i1){ for (int i = 0; i < dirs.length-1;i++) { ret=new File(ret, dirs[i]); } if(!ret.exists()) ret.mkdirs(); ret=new File(ret, dirs[dirs.length-1]); return ret; } return ret; } public static void deleteDirFile(String path){ File file=new File(path); if(file.isDirectory()){ //如果是目录,先递归删除 String[] list=file.list(); for(int i=0;i 0){ pdfNames.substring(0,pdfNames.length()-1); } return pdfNames; } public static void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数文件大小 fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("copy file error!"); e.printStackTrace(); } } public static boolean fileExist(String fileNames,String pdfName){ boolean flag=false; if(!"".equals(fileNames)){ String[] nameArr=fileNames.split(","); for(int i=0;i 看完了这篇文章,相信你对“java如何实现多个文件压缩成压缩包”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
名称栏目:java如何实现多个文件压缩成压缩包-创新互联
当前网址:http://bjjierui.cn/article/gpiig.html