java实现文件打包压缩处理
2012-02-13 17:29
656 查看
package testZip; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipMultiDirectoryCompress { public static String getFileModiefLast(File file){ Date d= new Date(file.lastModified()); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String date = df.format(d); System.out.println("FileModieflast---"+date); return date; } public static void main(String[] args) { //初始化支持多级目录压缩的ZipMultiDirectoryCompress ZipMultiDirectoryCompress zipCompress = new ZipMultiDirectoryCompress(); //压缩目录,可以指向一个文件 String directory = "D:\\workspace\\JMS\\WebRoot\\中文文件.txt"; //生成的压缩文件 String destFile = "D:\\workspace\\JMS\\WebRoot\\中文.zip"; //默认的相对地址,为根路径 String defaultParentPath = ""; ZipOutputStream zos = null; try { //创建一个Zip输出流 zos = new ZipOutputStream(new FileOutputStream(destFile)); //启动压缩进程 zipCompress.startCompress(zos,defaultParentPath,directory); } catch (FileNotFoundException e){ e.printStackTrace(); } finally{ try { if(zos != null)zos.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 开始压缩. * * @param zos ZipOutputStream * @param oppositePath 相对路径,开始时相对路径为空 * @param directory 压缩目录,也可能是文件 */ public void startCompress(ZipOutputStream zos, String oppositePath, String directory){ File file = new File(directory); if(file.isDirectory()){ //如果是压缩目录 File[] files = file.listFiles(); for(int i=0;i<files.length;i++){ File aFile = files[i]; if(aFile.isDirectory()){ //如果是目录,修改相对地址 String newOppositePath = oppositePath + aFile.getName() + "/"; //创建目录 compressDirectory(zos, oppositePath, aFile); //进行递归调用 startCompress(zos,newOppositePath,aFile.getPath()); } else { //如果不是目录,则进行压缩 compressFile(zos,oppositePath,aFile); } } } else { //如果是压缩文件,直接调用压缩方法进行压缩 compressFile(zos,oppositePath,file); } } /** * 压缩文件. * * @param zos ZipOutputStream * @param oppositePath 相对于路路径 * @param file 压缩的文件 */ public void compressFile(ZipOutputStream zos, String oppositePath, File file){ //创建一个Zip条目,每个Zip条目都是必须相对于根路径 ZipEntry entry = new ZipEntry(oppositePath + file.getName()); InputStream is = null; try{ //将条目保存到Zip压缩文件当中 zos.putNextEntry(entry); //从文件输入流当中读取数据,并将数据写到输出流当中. is = new FileInputStream(file); int length = 0; int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; while((length=is.read(buffer,0,bufferSize))>=0){ zos.write(buffer, 0, length); } zos.closeEntry(); }catch(IOException ex){ ex.printStackTrace(); } finally { try{ if(is != null)is.close(); }catch(IOException ex){ ex.printStackTrace(); } } } /** * 在压缩文件当中创建一个新的压缩目录,支持空目录. * * @param zos * @param oppositePath * @param file */ public void compressDirectory(ZipOutputStream zos, String oppositePath, File file){ //压缩目录,这是关键,创建一个目录的条目时,需要在目录名后面加多一个"/" ZipEntry entry = new ZipEntry(oppositePath + file.getName() + "/"); try { zos.putNextEntry(entry); zos.closeEntry(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- 使用Java实现多个文件压缩打包
- java实现多文件压缩打包
- java 压缩打包文件的实现方法
- JAVA实现多文件压缩打包
- Java实现将多个文件打包压缩成tar.gz文件
- java实现将多个文件打包成zip压缩文件以及对压缩文件的加密
- Java实现多文件压缩打包的方法
- JAVA实现将文件或是文件夹打包压缩成zip格式
- Java处理压缩文件或文件夹相关的一些方法的实现
- java实现文件上传下载和图片压缩代码示例
- Java使用zip4j进行压缩文件处理
- 【转载】Java如何实现文件打包下载功能
- java实现文件压缩并加密-基于zip4j
- java将文件打包成ZIP压缩文件的工具类实例
- 用java代码实现文件的zip压缩与解压
- Java实现打包下载BLOB字段中的文件
- Java 实现文件批量文件打包下载
- java实现一次性压缩多个文件
- Java实现将一个文件进行压缩
- Java 实现压缩文件