ZIP按照目录结果打包整个文件目录

public boolean downZipOrder(String sourceUrl,String purposeUrl){
		boolean flag=false;
		// 初始化支持多级目录压缩的ZipMultiDirectoryCompress
		ZipMultiDirectoryCompress zipCompress = new ZipMultiDirectoryCompress();
		// 压缩目录,可以指向一个文件
	//	String directory = ExcelUtil.DOC_DIR ;
		// 生成的压缩文件
	//	String destFile = "d:/all.zip";
		// 默认的相对地址,为根路径
		long start = System.currentTimeMillis();
		ZipOutputStream zos = null;
		try {
			// 创建一个Zip输出流
			zos = new ZipOutputStream(new FileOutputStream(purposeUrl));
			zos.setEncoding("GBK");
			// 启动压缩进程
			zipCompress.startCompress(zos, sourceUrl);
			flag=true;
		} catch (FileNotFoundException e) {
			flag=false;
			e.printStackTrace();
		} finally {
			try {
				if (zos != null)
					zos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println(System.currentTimeMillis() - start);
		return flag;
	}

 

public void startCompress(ZipOutputStream zos, String directory) {
		startCompress(zos, StringUtils.EMPTY, 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()) {
					// 如果是目录,修改相对地址,apache解决了中文字符乱码问题。
					String newOppositePath = oppositePath + aFile.getName() + "/";
					// 创建目录
					compressDirectory(zos, oppositePath, aFile);
					// 进行递归调用
					startCompress(zos, newOppositePath, aFile.getPath());
				} else {
					// 如果不是目录,则进行压缩
					compressFile(zos, oppositePath, aFile);
				}
			}
		} else {
			// 如果是压缩文件,直接调用压缩方法进行压缩
			compressFile(zos, oppositePath, file);
		}
	}

	public void compressFile(ZipOutputStream zos, String oppositePath, File file) {
		// 创建一个Zip条目,每个Zip条目都是必须相对于根路径
		ZipEntry entry = new ZipEntry(oppositePath + file.getName());
		InputStream is = null;
		try {
			entry.setUnixMode(644);
			// 将条目保存到Zip压缩文件当中
			zos.putNextEntry(entry);
			// 从文件输入流当中读取数据,并将数据写到输出流当中.
			is = new FileInputStream(file);
			int length = 0;
			byte[] buffer = new byte[BUF_SIZE];
			while ((length = is.read(buffer, 0, BUF_SIZE)) >= 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();
			}
		}
	}

	public void compressDirectory(ZipOutputStream zos, String oppositePath, File file) {
		// 压缩目录,这是关键,创建一个目录的条目时,需要在目录名后面加多一个"/"
		ZipEntry entry = new ZipEntry(oppositePath + file.getName() + "/");
		try {
			entry.setUnixMode(755);
			zos.putNextEntry(entry);
			zos.closeEntry();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值