package com.iss.iaf.installer.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; import javax.swing.JOptionPane; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; /** * 操作文件夹 * @author issuser * */ public class FileUtil { private static ArrayList<String> filelist = new ArrayList<String>(); /** * 通过递归得到某一路径下所有的目录及其文件的文件名 * @param filePath 文件路径 * @return */ public static ArrayList<String> getFiles(String filePath){ File root = new File(filePath); File[] files = root.listFiles(); for(File file:files){ if(file.isDirectory()){ /* * 递归调用 */ getFiles(file.getAbsolutePath()); filelist.add(file.getName()); System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath()); System.out.println(file.getName()); }else{ filelist.add(file.getName()); System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath()); System.out.println(file.getName()); } } return filelist; } /** * 获取zip压缩文件下所有的子文件名称 * @param zipFilePath 压缩文件路径 * @return */ public ArrayList<String> zipFileRead(String zipFilePath) { File file = new File(zipFilePath); ArrayList<String> fileNameList =new ArrayList<String>(); if (file.exists()) {//存在文件时 try { // 获得zip信息 ZipFile zipFile = new ZipFile(zipFilePath); //遍历zipFile中所有的实体 @SuppressWarnings("unchecked") Enumeration<? extends ZipEntry> entrys = zipFile.getEntries(); while (entrys.hasMoreElements()) { ZipEntry zipElement = entrys.nextElement(); String filePath = zipElement.getName(); //压缩包下的文件路径 if (filePath != null && filePath.indexOf(".") != -1) {// 是否为文件 int number =filePath.lastIndexOf("/"); String fileName = filePath.substring(number+1); fileNameList.add(fileName); }else{ continue; } } zipFile.close(); } catch (Exception e) { e.printStackTrace(); } } return fileNameList; } /** * 单文件下载 * @param from_file_name 文件名 * @param path 源文件路径 * @param to_path 目标文件路径 */ public void download(String from_file_name,String path,String to_path) { File dir = new File(to_path); if(!dir.exists() ){ //判断是否存在,不存在就创建 dir.mkdirs(); //创建文件夹 } OutputStream output = null; FileInputStream input = null; try { input = new FileInputStream(path); byte[] buffer = new byte[1024]; File des = new File(to_path, from_file_name); output = new FileOutputStream(des); int len = 0; while (-1 != (len = input.read(buffer))) { output.write(buffer, 0, len); } if (output != null) { try { if (input != null) output.close(); input.close(); // JOptionPane.showMessageDialog(null, "下载成功", "提示", 2); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "下载失败,请确认文件是否已上传状态!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } catch (IOException e1) { JOptionPane.showMessageDialog(null, "下载失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } /** * 下载文件夹下的所有文件 * * @param src * @param des */ public void downloadAll(String src, String des) { File file1 = new File(src); File[] fs = file1.listFiles(); File file2 = new File(des); if (!file2.exists()) { file2.mkdirs(); } for (File f : fs) { if (f.isFile() && f.getAbsolutePath().contains(".zip")) {//是文件并且是zip文件 download(f.getName(),f.getPath(), des+"\\");// 调用文件拷贝的方法 } else if (f.isDirectory()) { downloadAll(f.getPath(), des + "\\" ); } } } /** * 将单个zip文件解压 * @param zipFileName zip文件名 * @param path zip文件路径 * @param to_path 目标路径 */ public void getZipDecompression(String zipFileName, String path,String to_path){ to_path = to_path+"\\"+zipFileName; File dir = new File(to_path); if (!dir.exists()) { dir.mkdirs(); } ZipFile zipFile = null; try{ zipFile = new ZipFile(path); //遍历zipFile中所有的实体 @SuppressWarnings("unchecked") Enumeration<? extends ZipEntry> entrys = zipFile.getEntries(); while (entrys.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) entrys.nextElement(); String outFileName = to_path +"\\"+ zipEntry.getName(); File f = new File(outFileName); if(zipEntry.isDirectory()){ if(!f.exists()){ f.mkdirs(); } }else{ File pf = f.getParentFile(); if(!pf.exists()){ pf.mkdirs(); } InputStream in = zipFile.getInputStream(zipEntry); OutputStream out = new BufferedOutputStream( new FileOutputStream(f)); byte[] buffer = new byte[2048]; int nBytes = 0; while ((nBytes = in.read(buffer)) > 0) { out.write(buffer, 0, nBytes); } out.flush(); out.close(); in.close(); } } }catch(Exception e){ System.out.println("解压"+zipFileName+"出错---"+e.getMessage()); }finally{ if(zipFile!=null){ try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 解压所以zip下的所有文件 * * @param src * @param des */ public void getZipDecompressionAll(String src, String des) { File file1 = new File(src); File[] fs = file1.listFiles(); File file2 = new File(des); if (!file2.exists()) { file2.mkdirs(); } for (File f : fs) { if (f.isFile() && f.getAbsolutePath().contains(".zip")) {//是文件并且是zip文件 getZipDecompression(f.getName(),f.getPath(), des+"\\");// 调用文件拷贝的方法 } else if (f.isDirectory()) { getZipDecompressionAll(f.getPath(), des + "\\" ); } } } }
|