123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.hcks.cmfds.commons.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.zip.ZipOutputStream;
- public class DownLoadZip {
- // 创建文件夹
- public File createDir(String path) {
- File dirFile = null;
- try {
- dirFile = new File(path);
- if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
- dirFile.mkdirs();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return dirFile;
- }
- // 创建文件
- public File createFile(String path) {
- File file = new File(path);
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return file;
- }
- // 删除文件
- public boolean delFile(String path) throws Exception {
- boolean result = false;
- File file = new File(path);
- if (file.exists() && file.isFile()) {
- file.delete();
- result = true;
- }
- return result;
- }
- // 删除文件及文件夹
- public boolean delDir(File folder) {
- boolean result = false;
- try {
- String childs[] = folder.list();
- if (childs == null || childs.length <= 0) {
- if (folder.delete()) {
- result = true;
- }
- } else {
- for (int i = 0; i < childs.length; i++) {
- String childName = childs[i];
- String childPath = folder.getPath() + File.separator + childName;
- File filePath = new File(childPath);
- if (filePath.exists() && filePath.isFile()) {
- if (filePath.delete()) {
- result = true;
- } else {
- result = false;
- break;
- }
- } else if (filePath.exists() && filePath.isDirectory()) {
- if (delDir(filePath)) {
- result = true;
- } else {
- result = false;
- break;
- }
- }
- }
- }
- folder.delete();
- } catch (Exception e) {
- e.printStackTrace();
- result = false;
- }
- return result;
- }
- /*
- * @param zipFilePath
- * 打包文件存放路径
- * @param inputFolderName
- * 需要打包的文件夹
- * @throws Exception
- */
- public void zip(String zipFilePath, String inputFolderName)
- throws Exception {
- String zipFileName = zipFilePath; // 打包后文件名字
- File zipFile = new File(inputFolderName);
- zip(zipFileName, zipFile);
- }
-
- private void zip(String zipFileName, File inputFolder) throws Exception {
- FileOutputStream fileOut = new FileOutputStream(zipFileName);
- ZipOutputStream out = new ZipOutputStream(fileOut);
- zip(out, inputFolder, "");
- out.close();
- fileOut.close();
- }
- private void zip(ZipOutputStream out, File inputFolder, String base)
- throws Exception {
- if (inputFolder.isDirectory()) {
- File[] fl = inputFolder.listFiles();
- out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
- base = base.length() == 0 ? "" : base + "/";
- for (int i = 0; i < fl.length; i++) {
- zip(out, fl[i], base + fl[i].getName());
- }
- } else {
- out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
- FileInputStream in = new FileInputStream(inputFolder);
- int b;
- //System.out.println(base);
- while ((b = in.read()) != -1) {
- out.write(b);
- }
- in.close();
- }
- }
- }
|