java ZipInput|output-->Stream的使用

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.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZIPTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		List<String> fileList = new ArrayList<String>();
		fileList.add("D:\\zh.txt");
		fileList.add("D:\\a.java");
		fileList.add("D:\\a.class");
		fileList.add("D:\\aa.txt");
		fileList.add("D:\\图片.png");
		String fileDest = "D:\\中文.zip";
		try {
			// zip(fileList, fileDest);
		} catch (Exception e) {

			e.printStackTrace();
		}
		try {
			//unzip(fileDest, "E:/aa/bb", false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(ZIPTest.class.getResource("/"));

	}

	public static void zip(List<String> fileList, String fileDest)
			throws Exception {
		/**
		 * 三种情况 1.zip文件存在就不创建 2.如果zip文件不存在就创建 3.创建过程中有错;就删除
		 * 
		 */
		if (fileList == null || fileList.size() <= 0) {
			throw new Exception("file list is empty!");

		} else {
			File destFile = null;
			OutputStream output = null;
			ZipOutputStream zipOut = null;
			try {
				destFile = new File(fileDest);
				if (destFile.exists()) {

					throw new Exception("zip file exists!");

				} else {
					output = new FileOutputStream(destFile);
					// path
					byte[] buf = new byte[1024];
					zipOut = new ZipOutputStream(output);
					for (String filePath : fileList) {
						File file = new File(filePath);
						if (file.exists()) {

							// is file
							if (file.isFile()) {
								FileInputStream in = new FileInputStream(
										filePath);
								int index = filePath.lastIndexOf("/");
								if (index <= 0) {
									index = filePath.lastIndexOf("\\");

								}
								if (index <= 0) {

									destFile.delete();
									throw new Exception("file format Error!");
								}
								String filename = filePath.substring(index + 1);
								ZipEntry entry = new ZipEntry(filename);
								zipOut.putNextEntry(entry);
								int len = 0;
								while ((len = in.read(buf)) > 0) {
									zipOut.write(buf, 0, len);
								}
								in.close();

							} else {
								if (zipOut != null) {
									zipOut.closeEntry();
									try {
										zipOut.close();
									} catch (Exception e) {
										destFile.delete();
										e.printStackTrace();
									}
									zipOut = null;
								}
								if (output != null) {
									try {
										output.flush();
										output.close();
									} catch (Exception e) {
										destFile.delete();
									}
									output = null;

								}
								destFile.delete();
								throw new Exception("is not file!");

							}
						} else {
							if (zipOut != null) {
								zipOut.closeEntry();
								try {
									zipOut.close();
								} catch (Exception e) {
									destFile.delete();
									e.printStackTrace();
								}
								zipOut = null;
							}
							if (output != null) {
								try {
									output.flush();
									output.close();
								} catch (Exception e) {
									destFile.delete();
								}
								output = null;

							}
							destFile.delete();
							throw new Exception("file path not exists!");

						}
					}

				}

			} catch (Exception e1) {

				if (zipOut != null) {
					zipOut.closeEntry();
					try {
						zipOut.close();
					} catch (Exception e) {
						destFile.delete();
						e.printStackTrace();
					}
					destFile.delete();
					zipOut = null;
				}
				if (output != null) {
					try {
						output.flush();
						output.close();
					} catch (Exception e) {
						destFile.delete();
						e.printStackTrace();
					}
					output = null;
					destFile.delete();
				}

				e1.printStackTrace();

			}
			if (zipOut != null) {
				zipOut.closeEntry();
				try {
					zipOut.close();
				} catch (Exception e) {
					destFile.delete();
					e.printStackTrace();
				}
				zipOut = null;
			}
			if (output != null) {
				try {
					output.flush();
					output.close();
				} catch (Exception e) {
					destFile.delete();
					e.printStackTrace();
				}
				output = null;

			}

		}

	}

	public static void unzip(String zipPath, String fileDest, boolean originDect)
			throws Exception {
		File srcFile = new File(zipPath);
		if (srcFile.exists()) {

			if (srcFile.isFile()) {

				File destFile = new File(fileDest);
				if (destFile.exists()) {

					if (!destFile.isFile()) {

						InputStream input = new FileInputStream(srcFile);
						ZipInputStream zipInput = new ZipInputStream(input);
						ZipEntry zipEntry = null;
						while ((zipEntry = zipInput.getNextEntry()) != null) {

							String name = zipEntry.getName();

							String absFile = fileDest + "/" + name; // 不以原文件名为目标文件目录

							if (originDect) {
								int index = 0;
								index = srcFile.getName().lastIndexOf(".");
								if (index <= 0) {

									throw new Exception("target path Error!");

								}

								absFile = fileDest + "/"
										+ srcFile.getName().substring(0, index);

								File file = new File(absFile);
								if (!file.exists()) {

									file.mkdirs();
									absFile = fileDest
											+ "/"
											+ srcFile.getName().substring(0,
													index) + "/" + name;
								} else {

									absFile = fileDest
											+ "/"
											+ srcFile.getName().substring(0,
													index) + "/" + name;
								}

							}

							OutputStream output = new FileOutputStream(absFile);

							byte[] buf = new byte[1024];
							int len = 0;
							while ((len = zipInput.read(buf)) > 0) {
								output.write(buf, 0, len);
							}
							output.flush();
							output.close();

						}

					} else {

						throw new Exception("target path must is directory!");
					}

				} else {

					throw new Exception("target file directory must exists!");
				}

			} else {

				throw new Exception("file is not file!");
			}

		} else {
			throw new Exception("file is not exists!");

		}

	}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值