Home > Java > javaTutorial > How to compress and decompress zip files in Java

How to compress and decompress zip files in Java

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-14 23:01:01
forward
1709 people have browsed it

Code:

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

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

public class ZipUtil {

 

    /**

     * 压缩文件/文件夹

     *

     */

    public static void compress(String srcFilePath, String destFilePath) {

        File src = new File(srcFilePath);

        if (!src.exists()) {

            throw new RuntimeException(srcFilePath + "不存在");

        }

        File zipFile = new File(destFilePath);

        try {

            FileOutputStream fos = new FileOutputStream(zipFile);

            CheckedOutputStream cos new CheckedOutputStream(fos, new CRC32());

            ZipOutputStream zos = new ZipOutputStream(cos);

            String baseDir = "";

            compressbyType(src, zos, baseDir);

            zos.close();

        catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

 

    private static void compressbyType(File src, ZipOutputStream zos, String baseDir) {

        if (!src.exists())

            return;

        System.out.println("压缩" + baseDir + src.getName());

        if (src.isFile()) {

            compressFile(src, zos, baseDir);

        else if (src.isDirectory()) {

            compressDir(src, zos, baseDir);

        }

    }

 

 

    /**

     * 压缩文件

     */

    private static void compressFile(File file, ZipOutputStream zos, String baseDir) {

        if (!file.exists())

            return;

        try {

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

            ZipEntry entry = new ZipEntry(baseDir + file.getName());

            zos.putNextEntry(entry);

            int count;

            byte[] buf = new byte[8019];

            while ((count = bis.read(buf)) != -1) {

                zos.write(buf, 0, count);

            }

            bis.close();

        catch (Exception e) {

            // TODO: handle exception

        }

    }

 

 

    /**

     * 压缩文件夹

     *

     */

    private static void compressDir(File dir, ZipOutputStream zos, String baseDir) {

        if (!dir.exists())

            return;

        File[] files = dir.listFiles();

        if(files.length == 0){

            try {

                zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator));

            catch (IOException e) {

                e.printStackTrace();

            }

        }

        for (File file : files) {

            compressbyType(file, zos, baseDir + dir.getName() + File.separator);

        }

    }

 

 

    /**

     * 解压文件/文件夹

     */

    public static void decompress(String srcPath, String dest) throws Exception {

        File file = new File(srcPath);

        if (!file.exists()) {

            throw new RuntimeException(srcPath + "所指文件不存在");

        }

        ZipFile zf = new ZipFile(file);

        Enumeration entries = zf.entries();

        ZipEntry entry = null;

        while (entries.hasMoreElements()) {

            entry = (ZipEntry) entries.nextElement();

            System.out.println("解压" + entry.getName());

            if (entry.isDirectory()) {

                String dirPath = dest + File.separator + entry.getName();

                File dir = new File(dirPath);

                dir.mkdirs();

            else {

                // 表示文件

                File f = new File(dest + File.separator + entry.getName());

                if (!f.exists()) {

                    //String dirs = FileUtils.getParentPath(f);

                    String dirs = f.getParent();

                    File parentDir = new File(dirs);

                    parentDir.mkdirs();

                }

                f.createNewFile();

                // 将压缩文件内容写入到这个文件中

                InputStream is = zf.getInputStream(entry);

                FileOutputStream fos = new FileOutputStream(f);

                int count;

                byte[] buf = new byte[8192];

                while ((count = is.read(buf)) != -1) {

                    fos.write(buf, 0, count);

                }

                is.close();

                fos.close();

            }

        }

    }

 

    public static void main(String[] args) throws Exception {

//        compress("/Users/liyinlong/lyl/test","/Users/liyinlong/lyl/test.zip");

 

        decompress("/Users/liyinlong/lyl/test.zip""/Users/liyinlong/lyl/test");

    }

}

Copy after login

The above is the detailed content of How to compress and decompress zip files in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template