文件压缩工具类

package com.sinotrans.filesystem.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CustomizationZipUtil {

	protected static final Log log = LogFactory
			.getLog(CustomizationZipUtil.class);

	/**
	 * @描述 压缩文件
	 * @param srcFiles
	 *            压缩路径
	 * @param zipPath
	 *            目标全路径
	 */
	public static void zipFileWithTier(String srcFiles, String zipPath) {
		try {

			FileOutputStream zipFile = new FileOutputStream(zipPath);
			BufferedOutputStream buffer = new BufferedOutputStream(zipFile);
			ZipOutputStream out = new ZipOutputStream(buffer);
			zipFiles(srcFiles, out, "");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("", e);
		}
	}

	
	public static void zipFiles(String filePath, ZipOutputStream out,
			String prefix) throws IOException {
		File file = new File(filePath);
		if (file.isDirectory()) {
			if (file.listFiles().length == 0) {
				ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "/");
				out.putNextEntry(zipEntry);
				out.closeEntry();
			} else {
				prefix += file.getName() + File.separator;
				for (File f : file.listFiles())
					zipFiles(f.getAbsolutePath(), out, prefix);
			}
		} else {
			FileInputStream in = new FileInputStream(file);
			ZipEntry zipEntry = new ZipEntry(prefix + file.getName());
			out.putNextEntry(zipEntry);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			out.closeEntry();
			in.close();
		}

	}

	/**
	 * @描述 解压缩文件
	 * @param zipFilePath
	 *            待解压文件路径
	 * @param prefix
	 *            目标路径
	 * @throws IOException
	 */
	public static void unzipFilesWithTier(String zipFilePath, String prefix)
			throws IOException {
		byte[] bytes = readFileByte(zipFilePath);
		prefix = prefix + File.separator;
		InputStream bais = new ByteArrayInputStream(bytes);
		ZipInputStream zin = new ZipInputStream(bais);
		ZipEntry ze;
		while ((ze = zin.getNextEntry()) != null) {
			if (ze.isDirectory()) {
				File file = new File(prefix + ze.getName());
				if (!file.exists())
					file.mkdirs();
				continue;
			}
			File file = new File(prefix + ze.getName());
			if (!file.getParentFile().exists())
				file.getParentFile().mkdirs();
			ByteArrayOutputStream toScan = new ByteArrayOutputStream();
			byte[] buf = new byte[1024];
			int len;
			while ((len = zin.read(buf)) > 0) {
				toScan.write(buf, 0, len);
			}
			byte[] fileOut = toScan.toByteArray();
			toScan.close();
			writeByteFile(fileOut, new File(prefix + ze.getName()));
		}
		zin.close();
		bais.close();
	}

	@SuppressWarnings("resource")
	public static byte[] readFileByte(String filename) throws IOException {

		if (filename == null || filename.equals("")) {
			throw new NullPointerException("File is not exist!");
		}
		File file = new File(filename);
		long len = file.length();
		byte[] bytes = new byte[(int) len];

		BufferedInputStream bufferedInputStream = new BufferedInputStream(
				new FileInputStream(file));
		int r = bufferedInputStream.read(bytes);
		if (r != len)
			throw new IOException("Read file failure!");
		bufferedInputStream.close();

		return bytes;

	}

	public static String writeByteFile(byte[] bytes, File file) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(bytes);
		} catch (FileNotFoundException e) {
			log.error("", e);

		} catch (IOException e) {
			log.error("", e);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					log.error("", e);
				}
			}
		}
		return "success";
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值