JAVA zip 压缩包 导出

JAVA 导出 zip压缩文件,代码如下

public void downloading(String orderId) {
    
    
        List<String> urlList=new ArrayList<>();
        urlList.add("test/20210729/91ff23f577a422711b2cac7f6649c747.png");
        urlList.add("test/20210729/3edf79f04afb9a4202c76523200d6cb7.png");
        urlList.add("test/20210729/b7cd48017d698ea52a080ffc7323eaa3.jpg");
            // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
            String endpoint = OssUtils.End_Point;
            // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
            String accessKeyId = OssUtils.Key_Id;
            String accessKeySecret = OssUtils.Key_Secret;
            // 填写Bucket名称。
            String bucketName = OssUtils.BUCKET;
            //  D:\git\daotie\daotie
            // 填写不包含Bucket名称在内的Object完整路径,例如testfolder/exampleobject.txt。
            for (String s : urlList) {
    
    
                String objectName = s;
                //在填写文件路径时,一定要写上具体的文件名称(xx.txt),否则会出现拒绝访问。
                String[] split = objectName.split("/");
                File file = new File(filePath + "/" + split[split.length - 1]);
                file.setWritable(true, false);
                if (!file.exists()) {
    
    
                    //先得到文件的上级目录,并创建上级目录,在创建文件
                    file.getParentFile().mkdir();
                    try {
    
    
                        //创建文件
                        file.createNewFile();
                    } catch (IOException e) {
    
    
                        e.printStackTrace();
                    }
                }
                // 创建OSSClient实例。
                OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                // 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
                // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
                ossClient.getObject(new GetObjectRequest(bucketName, objectName), file);
                // 关闭OSSClient。
                ossClient.shutdown();
            }
        	String downloadName = 11111 + ".zip";


            //将文件进行打包下载
            try {
    
    
                OutputStream out = response.getOutputStream();
                byte[] data = createZip(filePath);//服务器存储地址
                response.reset();
                response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName);
                response.addHeader("Content-Length", "" + data.length);
                response.setContentType("application/octet-stream;charset=UTF-8");
                IOUtils.write(data, out);
                out.flush();
                out.close();
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
        delZSPic(filePath);
    }
    private void delZSPic(String filePath) {
    
    
        if(filePath != null) {
    
    
            File file = new File(filePath);
            if(file.exists()) {
    
    
                File[] filePaths = file.listFiles();
                for(File f : filePaths) {
    
    
                    if(f.isFile()) {
    
    
                        f.delete();
                    }
//                    if(f.isDirectory()){
    
    
//                        String fpath = f.getPath();
//                        delZSPic(fpath);
//                        f.delete();
//                    }
                }
            }
        }
    }
    public byte[] createZip(String srcSource) throws Exception {
    
    
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        //将目标文件打包成zip导出
        File file = new File(srcSource);
        zip(zip, file, "");
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    public void zip(ZipOutputStream zip, File file, String dir) throws Exception {
    
    
        //如果当前的是文件夹,则进行进一步处理
        if (file.isDirectory()) {
    
    
            //得到文件列表信息
            File[] files = file.listFiles();
            //将文件夹添加到下一级打包目录
//            zip.putNextEntry(new ZipEntry(dir + "/"));
            dir = dir.length() == 0 ? "" : dir + "/";
            //循环将文件夹中的文件打包
            for (int i = 0; i < files.length; i++) {
    
    
                zip(zip, files[i], dir + files[i].getName());         //递归处理
            }
        } else {
    
       //当前的是文件,打包处理
            //文件输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(dir);
            zip.putNextEntry(entry);
            zip.write(FileUtils.readFileToByteArray(file));
            IOUtils.closeQuietly(bis);
            zip.flush();
            zip.closeEntry();
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_45818787/article/details/119861932