We often use compression software such as WinZIP to compress files for easy transmission. Java also provides a class that compresses files to reduce the amount of data during transmission, which can easily compress files into ZIP, JAR, GZIP, etc. GZIP is mainly a compressed file under the Linux system.
The following mainly talks about compressed files in ZIP form, and compressed files in JAR and GZIP form are also used similarly.
ZIP is a very common form of compression. In Java, to implement ZIP compression, the main use of the classes in the java.util.zip package. Mainly include ZipFile, ZipOutputStream, ZipInputStream and ZipEntry. ZipOutputStream is used to compress files, ZipInputStream and ZipFile are used to decompress files, and ZipEntry is used during compression and decompression. In Java Zip compressed files, each subfile is a ZipEntry object.
Compressed files:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.nio.charset.Charset;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipOutputStreamTest { public static void main(String args[]) throws IOException { test1(); test2(); } public static void test1() throws IOException { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D://testZip.zip"), Charset.forName("GBK")); //Instantiate a ZipEntry object named ab.txt ZipEntry entry = new ZipEntry("ab.txt"); //Set comment zos.setComment("zip test for single file"); //Add the generated ZipEntry object into the compressed file, and then the content written into the compressed file will be placed in this ZipEntry object zos.putNextEntry(entry); InputStream is = new FileInputStream("D://ab.txt"); int len = 0; while ((len = is.read()) != -1) zos.write(len); is.close(); zos.close(); } public static void test2() throws IOException { File inFile = new File("D://test"); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D://test.zip"), Charset.forName("GBK")); zos.setComment("Multi-file processing"); zipFile(inFile, zos, ""); zos.close(); } public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException { if (inFile.isDirectory()) { File[] files = inFile.listFiles(); for (File file:files) zipFile(file, zos, dir + "//" + inFile.getName()); } else { String entryName = null; if (!"".equals(dir)) entryName = dir + "//" + inFile.getName(); else entryName = inFile.getName(); ZipEntry entry = new ZipEntry(entryName); zos.putNextEntry(entry); InputStream is = new FileInputStream(inFile); int len = 0; while ((len = is.read()) != -1) zos.write(len); is.close(); } } } Unzip the file:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.charset.Charset;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipInputStream;public class ZipInputStreamTest { public static void main(String args[]) throws IOException { File file = new File("D://test.zip");//Compress file ZipFile zipFile = new ZipFile(file);//Instantiate ZipFile, each zip compressed file can be represented as a ZipFile //Instantiate a ZipInputStream object of a Zip compressed file, you can use the getNextEntry() method of this class to get each ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK")); ZipEntry zipEntry = null; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String fileName = zipEntry.getName(); File temp = new File("D://unpackTest//" + fileName); if (! temp.getParentFile().exists()) temp.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(temp); //Get the specific ZipEntry input stream through ZipFile's getInputStream method InputStream is = zipFile.getInputStream(zipEntry); int len = 0; while ((len = is.read()) != -1) os.write(len); os.close(); is.close(); } zipInputStream.close(); } }The above is a compilation of the information on Java compression and decompression files. We will continue to add relevant information in the future. Thank you for your support for this website!