发个文件压缩解压的代码吧。

我是代码派
public class ZipUtils {

	/**
	 * 解压文件
	 * @param zipPath
	 * @param toPath
	 * @throws IOException
	 */
	public static void unCompress(String zipPath, String toPath) throws IOException {
		if(!toPath.endsWith("\\"))toPath += "\\";
		File destFile = new File(toPath);
		if(!destFile.exists())destFile.mkdirs();
		File zipfile = new File(zipPath);
		ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
		ZipEntry entry = null;
		while ((entry = zis.getNextEntry()) != null) {
			if (entry.isDirectory()) {
				File file = new File(toPath + entry.getName() + "\\");
				file.mkdirs();
			} else {
				File file = new File(toPath + entry.getName());
				if(!file.getParentFile().exists())file.getParentFile().mkdirs();
				FileOutputStream fos = new FileOutputStream(file);
				byte buf[] = new byte[1024];
				int len = -1;
				while ((len = zis.read(buf, 0, 1024)) != -1) {
					fos.write(buf, 0, len);
				}
				fos.close();
			}
		}
		zis.close();
	}
	
	public static byte[] getData(String zipPath, String path) throws IOException{
		File zipfile = new File(zipPath);
		ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ZipEntry entry = null;
		while ((entry = zis.getNextEntry()) != null) {
			if(entry.getName().equals(path)){
				byte buf[] = new byte[1024];
				int len = -1;
				while ((len = zis.read(buf, 0, 1024)) != -1) {
					baos.write(buf, 0, len);
				}
				break;
			}
		}
		baos.close();
		zis.close();
		return baos.toByteArray();
	}

	/**
	 * 压缩单个文件
	 * @param file
	 */
	public static void compress(File file) {
		try {
			String fileName = file.getName();
			if(fileName.indexOf(".") != -1)
				fileName = fileName.substring(0, fileName.indexOf("."));
			FileOutputStream f = new FileOutputStream(file.getParent() + "/" + fileName+ ".zip");
			CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
			InputStream in = new FileInputStream(file);
			out.putNextEntry(new ZipEntry(file.getName()));
			int len = -1;
			byte buf[] = new byte[1024];
			while((len = in.read(buf, 0, 1024)) != -1)
				out.write(buf, 0, len);
			out.closeEntry();
			
			in.close();
			out.close();
		} catch (Exception e) {
			System.err.println(e);
		}
	}

	/**
	 * 压缩文件夹
	 * @param file
	 * @throws IOException
	 */
	public static void compressDir(File file) throws IOException{
		FileOutputStream f = new FileOutputStream(file.getParent() + file.getName() + ".zip");
		CheckedOutputStream cs = new CheckedOutputStream(f, new Adler32());
		ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
		
		compressDir(file, out, file.getAbsolutePath());
		
		out.flush();
		out.close();
	}
	
	/**
	 * 压缩文件夹递归调用方法
	 * @param file
	 * @param out
	 * @throws IOException
	 */
	private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException{
		if(srcFile.isDirectory()){
			File subfile[] = srcFile.listFiles();
			for(int i=0; i< subfile.length; i++){
				compressDir(subfile[i], out, destPath);
			}
		}else{
			InputStream in = new FileInputStream(srcFile);
			String name = srcFile.getAbsolutePath().replace(destPath, "");
			if(name.startsWith("\\"))name = name.substring(1);
			ZipEntry entry = new ZipEntry(name);
			entry.setSize(srcFile.length());
			entry.setTime(srcFile.lastModified());
			out.putNextEntry(entry);
			int len = -1;
			byte buf[] = new byte[1024];
			while((len = in.read(buf, 0, 1024)) != -1)
				out.write(buf, 0, len);
			
			out.closeEntry();
			in.close();
		}
	}
	
	public static void main(String[] args) throws Exception {
		ZipUtils.compress(new File("d:/aa/a01.jpg"));

//		try {
//			byte buf[] = ZipDemo.getData("d:/aa.zip", "aa.txt");
//			System.out.println(buf.length);
//			ZipDemo.unCompress("d:/aa.zip", "d:/kk/");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值