ZQN
2024-08-14 f6a1bf1d9b19dd8b3750034048f3876d086db1f1
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
package com.project.common.utils.zip;
 
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class ImageDownloader {
 
    public static void main(String[] args) throws IOException {
        File[] imageFiles = new File("images").listFiles();
 
        if (imageFiles != null) {
            File zipFile = new File("images.zip");
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
 
            for (File imageFile : imageFiles) {
                if (imageFile.isFile()) {
                    ZipArchiveEntry entry = new ZipArchiveEntry(imageFile.getName());
                    zos.putArchiveEntry(entry);
 
                    FileInputStream fis = new FileInputStream(imageFile);
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) != -1) {
                        zos.write(buffer, 0, length);
                    }
 
                    fis.close();
                    zos.closeArchiveEntry();
                }
            }
 
            zos.finish();
            zos.close();
            fos.close();
 
            System.out.println("Images downloaded and packed into 'images.zip' successfully.");
        } else {
            System.out.println("No images found.");
        }
    }
 
 
 
}