Core Java

Java Compression

In a recent project, we had to do something I had personally never really had to look at; Compression. We needed to take a couple files and images, zip them up and make them available for FTP, and yes somedays it does feel like we are back in the 90’s. Besides the FTP trip into the past its was good opportunity to spend a little bit of time on the subject.

Compressing Files

So above the usual IO classes BufferedInputStream, FileOutputStream and File there are:

  • ZipInputStream – An input stream for reading files in the ZIP file format. Zip entries are not cached, unlike ZipFile.
  • ZipOutputStream – An output stream for writing files in the ZIP file format. This has a default internal buffer of 512, a BufferedOutputStream can be used to increase this.
  • ZipEntry – Represents an entry int a zip file.
  • ZipFile – Used to read entries from a zip file. The entries are cached.
  • CRC32 – Used to compute the CRC-32 of a data stream.

Below is an example showing how to compress and decompress files in a folder, with and without a checksum:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package javaitzen.blog;
  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
  
/**
 * The Class FileCompressionUtil.
 */
public class FileCompressionUtil {
  
 private static final String PATH_SEP = "\\";
 public static final int BUFFER = 2048;
 private FileCompressionUtil() {}
   
 /**
  * Zip files in path.
  *
  * @param zipFileName the zip file name
  * @param filePath the file path
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public static void zipFilesInPath(final String zipFileName, final String filePath) throws IOException {
  final FileOutputStream dest = new FileOutputStream(zipFileName);
  final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
  try {
  
   byte[] data = new byte[BUFFER];
   final File folder = new File(filePath);
   final List< String > files = Arrays.asList(folder.list());
   for (String file : files) {
    final FileInputStream fi = new FileInputStream(filePath + PATH_SEP + file);
    final BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
    out.putNextEntry(new ZipEntry(file));
    int count;
    while ((count = origin.read(data, 0, BUFFER)) != -1) {
     out.write(data, 0, count);
    }
    origin.close();
    fi.close();
   }
  } finally {
   out.close();
   dest.close();
  }
 }
  
 /**
  * Zip with checksum. CRC32
  *
  * @param zipFileName the zip file name
  * @param folderPath the folder path
  * @return the checksum
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public static long zipFilesInPathWithChecksum(final String zipFileName, final String folderPath) throws IOException {
  
  final FileOutputStream dest = new FileOutputStream(zipFileName);
  final CheckedOutputStream checkStream = new CheckedOutputStream(dest, new CRC32());
  final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checkStream));
  try {
   byte[] data = new byte[BUFFER];
   final File folder = new File(folderPath);
   final List< String > files = Arrays.asList(folder.list());
   for (String file : files) {
    final FileInputStream fi = new FileInputStream(folderPath + PATH_SEP + file);
    final BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
    out.putNextEntry(new ZipEntry(file));
    int count;
    while ((count = origin.read(data, 0, BUFFER)) != -1) {
     out.write(data, 0, count);
    }
    origin.close();
   }
    
  } finally {
   out.close();
   checkStream.close();
   dest.flush();
   dest.close();
  }
  
  return checkStream.getChecksum().getValue();
 }
  
  
 /**
  * Unzip files to path.
  *
  * @param zipFileName the zip file name
  * @param fileExtractPath the file extract path
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public static void unzipFilesToPath(final String zipFileName, final String fileExtractPath) throws IOException {
  
  final FileInputStream fis = new FileInputStream(zipFileName);
  final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
  try {
   ZipEntry entry;
  
   while ((entry = zis.getNextEntry()) != null) {
    int count;
    byte[] data = new byte[BUFFER];
    final FileOutputStream fos = new FileOutputStream(fileExtractPath + PATH_SEP + entry.getName());
    final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
    while ((count = zis.read(data, 0, BUFFER)) != -1) {
     dest.write(data, 0, count);
    }
    dest.flush();
    dest.close();
   }
  } finally {
   fis.close();
   zis.close();
  }
  
 }
  
  
 /**
  * Unzip files to path with checksum. CRC32
  *
  * @param zipFileName the zip file name
  * @param fileExtractPath the file extract path
  * @param checksum the checksum
  * @return true, if checksum matches;
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public static boolean unzipFilesToPathWithChecksum(final String zipFileName, final String fileExtractPath, final long checksum) throws IOException {
  
  boolean checksumMatches = false;
  final FileInputStream fis = new FileInputStream(zipFileName);
  final CheckedInputStream checkStream = new CheckedInputStream(fis, new CRC32());
  final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checkStream));
  
  try {
  
   ZipEntry entry = null;
   while ((entry = zis.getNextEntry()) != null) {
    int count;
    byte[] data = new byte[BUFFER];
    final FileOutputStream fos = new FileOutputStream(fileExtractPath + PATH_SEP + entry.getName());
    final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
    while ((count = zis.read(data, 0, BUFFER)) != -1) {
     dest.write(data, 0, count);
    }
    dest.flush();
    dest.close();
   }
  
  } finally {
   zis.close();
   fis.close();
   checkStream.close();
  }
  
  if(checkStream.getChecksum().getValue() == checksum) {
   checksumMatches = true;
  }
    
  return checksumMatches;
 }
 
}

Compressing Objects

We didn’t end up using object compression but I had a look at it anyways. I did a little generic compress / expand util, don’t know if it will ever be useful. I left the input params as OutputStream and InputStream as this could theoretically be used with any stream implementation from socket communication to string manipulation.

The compression related classes being used here:

  • GZIPInputStream – An input stream filter for reading compressed data in the GZIP file format.
  • GZIPOutputStream – An output stream filter for writing compressed data in the GZIP file format.
  • Default internal buffer of 512, use BufferedOutputStream if you require more.
01
02
03
04
05
06
07
08
09
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
package javaitzen.blog;
  
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
  
/**
 * The Class ObjectCompressionUtil.
 *
 * @param <T> the generic type of the serializable object to be compressed
 */
public class ObjectCompressionUtil<T extends Serializable> {
  
 /**
  * Compress object.
  *
  * @param objectToCompress the object to compress
  * @param outstream the outstream
  * @return the compressed object
  * @throws IOException Signals that an I/O exception has occurred.
  */
 public T compressObject(final T objectToCompress, final OutputStream outstream) throws IOException {
  
  final GZIPOutputStream gz = new GZIPOutputStream(outstream);
  final ObjectOutputStream oos = new ObjectOutputStream(gz);
    
  try {
  oos.writeObject(objectToCompress);
  oos.flush();
  return objectToCompress;
  }finally {
   oos.close();
   outstream.close();
  }
  
 }
  
 /**
  * Expand object.
  *
  * @param objectToExpand the object to expand
  * @param instream the instream
  * @return the expanded object
  * @throws IOException Signals that an I/O exception has occurred.
  * @throws ClassNotFoundException the class not found exception
  */
 public T expandObject(final T objectToExpand, final InputStream instream) throws IOException,
   ClassNotFoundException {
  final GZIPInputStream gs = new GZIPInputStream(instream);
  final ObjectInputStream ois = new ObjectInputStream(gs);
  
  try {
   @SuppressWarnings("unchecked")
   T expandedObject = (T) ois.readObject();
   return expandedObject;
  } finally {
   gs.close();
   ois.close();
  }
 }
  
}

Reference: Java Compression from our JCG partner Brian at Zen in the art of IT.

Happy coding!

Byron

Related Articles :
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button