lxw
2023-10-09 1fa878f57239475c925ae1ea4bd2793c3e831b49
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
81
82
package com.whyc.util;
 
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import com.whyc.pojo.FileMessage;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipInputStream;
 
public class ZipAndRarUtil {
    public static List<FileMessage> getZipFileList(String zipFileName) throws IOException {
        ZipInputStream zipInputStream = null;
        try{
            FileInputStream inputStream = new FileInputStream(zipFileName);
            zipInputStream = new ZipInputStream(
                    new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
            java.util.zip.ZipEntry ze;
            List<FileMessage> list = new ArrayList<>();
            //循环遍历
            while ((ze = zipInputStream.getNextEntry()) != null) {
                FileMessage fileMessage=new FileMessage();
                if (!ze.isDirectory()) {
                    fileMessage.setFileName(ze.getName());
                    fileMessage.setFileSize(ze.getSize());
                    String formatted = ActionUtil.sdfwithALL.format( new Date( ze.getLastModifiedTime().toMillis() ) );
                    fileMessage.setFileTime(formatted);
                    list.add(fileMessage);
                }
            }
            return list;
        } catch (Exception e) {
            //logger.error(" getZipOneFileContent error:" + e.getMessage());
            return null;
        } finally {
            if (zipInputStream != null) {
                zipInputStream.closeEntry();
            }
        }
    }
 
    public static List<FileMessage> getRarList(String rarFileName) throws IOException {
        FileInputStream inputStream = null;
        Archive archive = null;
        try {
            inputStream = new FileInputStream(rarFileName);
            archive = new Archive(inputStream);
            FileHeader fileHeader = archive.nextFileHeader();
            List<FileMessage> list = new ArrayList<>();
            while (fileHeader != null) {
                if (fileHeader.isDirectory()) {
                    continue;
                }
                FileMessage fileMessage=new FileMessage();
                fileMessage.setFileName(fileHeader.getFileNameString());
                fileMessage.setFileSize(fileHeader.getFullUnpackSize());
                String formatted = ActionUtil.sdfwithALL.format(fileHeader.getMTime());
                fileMessage.setFileTime(formatted);
                list.add(fileMessage);
                fileHeader = archive.nextFileHeader();
            }
            return list;
        } catch (Exception e) {
            //logger.error(" getRarFileList error:" + e.getMessage());
            return null;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (archive != null) {
                archive.close();
            }
        }
    }
 
}