• 文件的工具类



    文件压缩和解压


    import java.io.*;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipOutputStream;
    
    public class FileZipOrUnZip {
    
        /**
         * @param sourceFullPath 源文件全路径
         * @param zipFullPath 压缩后的全路径
         * 压缩文件
         * @throws Exception
         */
        public static void zip(String sourceFullPath, String zipFullPath) throws Exception{
            System.out.println("压缩中...");
            ZipOutputStream out = new ZipOutputStream( new FileOutputStream(zipFullPath));
            File sourceFile = new File(sourceFullPath);
            compress(out,sourceFile,sourceFile.getName());
            out.close();
            System.out.println("压缩完成");
        }
    
        public static void compress(ZipOutputStream out, File sourceFile,String base) throws Exception{
            if (!sourceFile.isDirectory()) {
                out.putNextEntry( new ZipEntry(base) );
                FileInputStream fos = new FileInputStream(sourceFile);
                BufferedInputStream bis = new BufferedInputStream(fos);
                int tag;
                System.out.println(base);
                while((tag=bis.read())!=-1){
                    out.write(tag);
                }
                bis.close();
                fos.close();
                return;
            }
    
            File[] flist = sourceFile.listFiles();
            if(flist.length == 0){
                System.out.println(base+"/");
                out.putNextEntry(new ZipEntry(base+"/") );
                return;
            }
            for(int i=0;i<flist.length;i++){
                compress(out,flist[i],base+"/"+flist[i].getName());
            }
        }
    
    
    
    
        /**
         * zip解压
         * @param srcFile        zip源文件
         * @param destDirPath     解压后的目标文件夹
         */
        public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
            long start = System.currentTimeMillis();
            // 判断源文件是否存在
            if (!srcFile.exists()) {
                throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
            }
            // 开始解压
            ZipFile zipFile = null;
            try {
                zipFile = new ZipFile(srcFile);
                Enumeration<?> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry) entries.nextElement();
                    System.out.println("解压" + entry.getName());
                    // 如果是文件夹,就创建个文件夹
                    if (entry.isDirectory()) {
                        String dirPath = destDirPath + "/" + entry.getName();
                        File dir = new File(dirPath);
                        dir.mkdirs();
                    } else {
                        // 如果是文件,就先创建一个文件,然后用io流把内容copy过去
                        File targetFile = new File(destDirPath + "/" + entry.getName());
                        // 保证这个文件的父文件夹必须要存在
                        if(!targetFile.getParentFile().exists()){
                            targetFile.getParentFile().mkdirs();
                        }
                        targetFile.createNewFile();
                        // 将压缩文件内容写入到这个文件中
                        InputStream is = zipFile.getInputStream(entry);
                        FileOutputStream fos = new FileOutputStream(targetFile);
                        int len;
                        byte[] buf = new byte[1024];
                        while ((len = is.read(buf)) != -1) {
                            fos.write(buf, 0, len);
                        }
                        fos.close();
                        is.close();
                    }
                }
                long end = System.currentTimeMillis();
                System.out.println("解压完成,耗时:" + (end - start) +" ms");
            } catch (Exception e) {
                throw new RuntimeException("unzip error from ZipUtils", e);
            } finally {
                if(zipFile != null){
                    try {
                        zipFile.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
    //        zip("D:git_test	est", "D:git_test	est\test1.zip");
    
            unZip(new File("D:\git_test\test\test1.zip"), "D:\git_test\test");
        }
    
    }
  • 相关阅读:
    red hat linux下安装mysql
    oracle数据库sys与system默认密码
    eclipse 创建maven web项目
    mysql-5.7.17-winx64的安装配置
    有趣的浏览器地址栏JavaScript代码
    jsp自定义标签Tag
    exp:CollectionSecurity must be empty, but is not; jsp自定义标签异常
    java中&与&&的区别
    解决springmvc在multipart/form-data方式提交请求在过滤器Filter中获取不到参数的问题
    java itextpdf使用HTML模板生成pdf文件,并设置table
  • 原文地址:https://www.cnblogs.com/zengqinghong/p/12003197.html
Copyright © 2020-2023  润新知