挺好:
- package cn.test;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.Scanner;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- public class ZipTool {
- public static void main(String[] args) {
- ZipTool test = new ZipTool();
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请您选择操作:(A)压缩文件 \t(B)解压文件");
- System.out.println("\t\t\t说明:输入A或B");
- String operate = sc.nextLine();
- if ("A".equalsIgnoreCase(operate)) {
- System.out.println("注意:压缩后的zip文件不能存放在要压缩的文件夹中!");
- System.out.println("请输入要解压的文件位置(文件或文件夹):");
- String srcPath=sc.nextLine();
- System.out.println("请输入要解压到的位置(只能是文件夹):");
- String targetDirPath=sc.nextLine();
- System.out.println("请输入解压后的文件名(不含\".zip\"后缀):");
- String zipName=sc.nextLine();
- try {
- test.zip(srcPath, targetDirPath, zipName);
- System.out.println("操作成功!");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- } else if ("B".equalsIgnoreCase(operate)) {
- System.out.println("请输入要解压的zip文件地址:");
- String zipF=sc.nextLine();
- System.out.println("请输入解压后的文件存放的位置:");
- String targetPath =sc.nextLine();
- try {
- test.unzip(zipF, targetPath);
- System.out.println("操作成功!");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- } else {
- System.out.println("输入错误!");
- continue;
- }
- System.out.println("是否继续?输入Y继续,其他任意键回车后退出");
- if("Y".equalsIgnoreCase(sc.nextLine())){
- continue;
- }else{
- break;
- }
- }
- sc.close();
- }
- /**
- * 解压方式一,通过ZipFile的方式获取所有的ZipEntry条目,并通过zipFile.getInputStream(entry)方法获取流
- *
- * @param zipF
- * 源位置
- * @param target
- * 目标位置
- * @throws IOException
- * @throws Exception
- */
- private void unzip(String zipF, String targetPath) throws IOException {
- File target = new File(targetPath);
- // 获取zipFile
- ZipFile zipFile = null;
- try {
- zipFile = new ZipFile(zipF);
- } catch (Exception e) {
- throw new RuntimeException("读取Zip文件错误!");
- }
-
- // 通过zipfile获取所有的ZipEntry
- Enumeration<? extends ZipEntry> entries = zipFile.entries();
- // 创建目标文件夹
- if (!target.exists() || target.isFile())
- target.mkdirs();
- // 解压操作
- while (entries.hasMoreElements()) {
- ZipEntry entry = entries.nextElement();
- String entryName = entry.getName();
- // 当entry是文件夹时在target中创建对应的文件夹,是文件时创建对应的文件
- if (entry.isDirectory()) {
- File dir = new File(target, entryName);
- if (!dir.exists() || dir.isFile())
- dir.mkdirs();
- } else {
- File path = new File(target, entryName);
- File parent = path.getParentFile();
- if (!parent.exists() || parent.isFile())
- parent.mkdirs();
- // 流读写操作
- FileOutputStream fos = new FileOutputStream(path);
- InputStream in = zipFile.getInputStream(entry);
- byte[] b = new byte[1024];
- int len = 0;
- while ((len = in.read(b)) != -1) {
- fos.write(b, 0, len);
- }
- fos.close();
- in.close();
- }
- }
- }
- /**
- * 解压方式二:通过ZipInputStream的getNextEntry()的方法获取所有条目
- *
- * @param zipF
- * @param target
- * @throws Exception
- */
- private void unzip2(String zipF, String targetPath) throws Exception {
- File target = new File(targetPath);
- ZipInputStream zipIn = null;
- try {
- zipIn = new ZipInputStream(new FileInputStream(zipF));
- } catch (Exception e) {
- throw new RuntimeException("读取Zip文件错误!");
- }
- // 定义ZipEntry
- ZipEntry entry = null;
- // 创建目标文件夹
- if (!target.exists() || target.isFile())
- target.mkdirs();
- while ((entry = zipIn.getNextEntry()) != null) {
- String entryName = entry.getName();
- if (entry.isDirectory()) {
- File dir = new File(target, entryName);
- if (!dir.exists() || dir.isFile())
- dir.mkdirs();
- } else {
- File dir = new File(target, entryName);
- File parent = dir.getParentFile();
- if (!parent.exists() || parent.isFile())
- parent.mkdirs();
- // 流读写操作
- FileOutputStream fos = new FileOutputStream(dir);
- byte[] b = new byte[1024];
- int len = 0;
- while ((len = zipIn.read(b)) != -1) {
- fos.write(b, 0, len);
- }
- fos.close();
- }
- zipIn.closeEntry();// 关闭当前条目
- }
- zipIn.close();
- }
- /**
- *
- * @param srcPath
- * 源
- * @param targetDirPath
- * 压缩到的位置
- * @param zipName
- * 压缩后的文件名
- * @throws IOException
- */
- public void zip(String srcPath, String targetDirPath, String zipName)
- throws IOException {
- if(targetDirPath.startsWith(srcPath)){
- throw new RuntimeException("压缩后的zip文件不能存放在要压缩的文件夹中!");
- }
- File srcF = new File(srcPath);
- // 对源文件进行检查
- if (!srcF.exists()) {
- throw new RuntimeException("您输入的文件位置不存在!");
- }
- // 当目标目录不存在时创建
- File targetDir = new File(targetDirPath);
- if (!targetDir.exists()||targetDir.isFile()) {
- targetDir.mkdirs();
- }
- File targetF = new File(targetDir, zipName + ".zip");
- // 防止原已经存在的文件被覆盖,如a.zip存在时文件名为a(1).zip..
- int count = 0;
- while (targetF.exists() && !targetF.isDirectory()) {
- targetF = new File(targetDir, zipName + "(" + (++count) + ").zip");
- }
- // 压缩操作
- ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
- targetF));
- zip(srcF, zipOut, "");
- zipOut.close();// 关闭zip流
- if (count != 0) {
- System.out.println("由于文件夹中已经存在" + zipName + ".zip文件,系统自动分配文件文件名为:"
- + zipName + "(" + (count) + ").zip");
- }
- }
- /**
- * 压缩主方法
- *
- * @param srcF
- * 要压缩的源文件
- * @param zipOut
- * @param parentPath
- * 上级目录
- * @throws IOException
- */
- private void zip(File srcF, ZipOutputStream zipOut, String parentPath)
- throws IOException {
- if (srcF.isDirectory()) {
- // 下一条目的上一级
- parentPath = parentPath + srcF.getName() + "/";
- // 添加一条ZipEntry
- zipOut.putNextEntry(new ZipEntry(parentPath));
- zipOut.closeEntry();// 关闭ZipEntry
- zipOut.flush(); // 刷新
- // 用递归的方法继续寻找下级的文件
- File[] files = srcF.listFiles();
- for (File file : files) {
- zip(file, zipOut, parentPath);
- }
- } else {
- zipOut.putNextEntry(new ZipEntry(parentPath + srcF.getName()));
- // 将文件写入到Zip输出流
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(srcF);
- byte[] b = new byte[1024];
- int len = 0;
- while ((len = fis.read(b)) != -1) {
- zipOut.write(b, 0, len);
- }
- } catch (Exception e) {
- throw new RuntimeException("压缩文件失败!");
- } finally {
- if (fis != null)
- fis.close();
- }
- zipOut.closeEntry();
- zipOut.flush();
- }
- }
- }
复制代码 |