zoukankan      html  css  js  c++  java
  • JAVA中解压压缩包到制定文件夹工具方法

    最近项目中看到一个别人写的解压压缩文件的方法,感觉不错,特此记录下来,方便与以后遇到类似问题。

     // 第一个参数就是需要解压的文件,第二个就是解压的目录
        public static boolean upZipFile(String zipFile, String folderPath) {
            ZipFile zfile = null;
            try {
                zfile = new ZipFile(zipFile);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            Enumeration zList = zfile.entries();
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            while (zList.hasMoreElements()) {
                ze = (ZipEntry) zList.nextElement();
                if (ze.isDirectory()) {
                    String dirstr = folderPath + ze.getName();
                    dirstr.trim();
                    File f = new File(dirstr);
                    f.mkdir();
                    continue;
                }
                OutputStream os = null;
                FileOutputStream fos = null;
                File realFile = getRealFileName(folderPath, ze.getName());
                try {
                    fos = new FileOutputStream(realFile);
                } catch (FileNotFoundException e) {
                    return false;
                }
                os = new BufferedOutputStream(fos);
                InputStream is = null;
                try {
                    is = new BufferedInputStream(zfile.getInputStream(ze));
                } catch (IOException e) {
                    return false;
                }
                int readLen = 0;
                // 进行一些内容复制操作
                try {
                    while ((readLen = is.read(buf, 0, 1024)) != -1) {
                        os.write(buf, 0, readLen);
                    }
                } catch (IOException e) {
                    return false;
                }
                try {
                    is.close();
                    os.close();
                } catch (IOException e) {
                    return false;
                }
            }
            try {
                zfile.close();
            } catch (IOException e) {
                return false;
            }
            return true;
        }
  • 相关阅读:
    selenium1-安装
    jmeter9-图形监控
    InetAddress.getLocalHost().getHostAddress() 在macOS里面反应很慢
    自定义实现简易定时任务
    Redis性能监控
    Linux启动进程、线程数量查看及修改方式
    jmeter处理request和response
    centos安装netdata
    scp使用expect自动输入密码
    Linux系统下生成TPS,ResponseTime曲线图
  • 原文地址:https://www.cnblogs.com/zblwyj/p/11275137.html
Copyright © 2011-2022 走看看