rain
2024-07-19 725abac64efc098b9ecb7827429f3c47924a00b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.dji.sample.media.util;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
public class UnZipPhotoUtil {
 
    public static void downloadAndSaveImages(List<String> urls, String outputFolder) throws IOException {
        int count = 0;
        for (String imageUrl : urls) {
            try {
                // 下载图片
                URL url = new URL(imageUrl);
                BufferedImage image = ImageIO.read(url);
 
                // 保存图片
                File outputfile = new File(outputFolder, "image_" + (count++) + ".jpg");
                ImageIO.write(image, "jpg", outputfile);
                System.out.println("Saved: " + outputfile.getAbsolutePath());
            } catch (IOException e) {
                System.err.println("Failed to download image: " + imageUrl);
                e.printStackTrace();
                throw e;
            }
        }
    }
 
    public static void zipFolder(String srcFolder, String destZipFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(destZipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {
 
            File srcFile = new File(srcFolder);
            for (File file : Objects.requireNonNull(srcFile.listFiles())) {
                try (FileInputStream fis = new FileInputStream(file)) {
                    ZipEntry zipEntry = new ZipEntry(file.getName());
                    zos.putNextEntry(zipEntry);
 
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zos.write(bytes, 0, length);
                    }
                    zos.closeEntry();
                } catch (IOException e) {
                    System.err.println("Failed to zip file: " + file.getPath());
                    e.printStackTrace();
                    throw e;
                }
            }
        }
    }
 
    public static void cleanUp(String folderName) {
        File folder = new File(folderName);
        deleteFolder(folder);
    }
 
    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }
}