Home File Compress/Decompress Operations
Post
Cancel

File Compress/Decompress Operations

When we develop code with Java, we often work with files and different types of them.

In general, we need compression operations to keep the file content intact and to reduce the file size. Although the compression process has different methods in Java, the current and my own methods are as follows.

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
public static byte[] compress(byte[] input, String outputFileName) {
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) {

        ZipEntry zipEntry = new ZipEntry(outputFileName);
        zipOut.putNextEntry(zipEntry);
        zipOut.write(input);
        zipOut.finish();
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        log.error("Compress failed -> {}", ExceptionUtils.getStackTrace(e));
        throw new CompressFileIOException(e);
    }
}

public static byte[] decompress(byte[] input) {
    byte[] result;
    try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(input));
         ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        int len;
        byte[] buffer = new byte[1024];
        ZipEntry zipEntry = zis.getNextEntry();
        while (Objects.nonNull(zipEntry) && (len = zis.read(buffer)) > 0){
            bos.write(buffer, 0, len);
        }
        result = bos.toByteArray();
    }  catch (IOException e) {
        log.error("Decompress failed -> {}", ExceptionUtils.getStackTrace(e));
        throw new CompressFileIOException(e);
    }
    return result;
}
This post is licensed under CC BY 4.0 by the author.