This article mainly introduces the use of Java related classes to compress files or folders.
zlib is a data compression library designed to process pure data (regardless of the source of the data).
7z is a new compression format that has the highest compression ratio at present.
gzip is a file compression tool (or the compressed file format generated by the compression tool) and is designed to process individual files. gzip uses zlib when zipping data in a file. In order to save information related to file properties, gzip needs to save more header content in the compressed file (*.gz), and zlib does not need to consider this. But gzip is only suitable for a single file, so the suffixes we often see on UNIX/Linux are *.tar.gz or *.tgz, which means that multiple files are first packaged into a single file with tar, and then compressed with gzip.
Zip is a format suitable for compressing multiple files (the corresponding tools include PkZip and WinZip, etc.). Therefore, the zip file must further contain information about the file directory structure, which is more information than gzip's header. However, it should be noted that the zip format can adopt a variety of compression algorithms. Most of our common zip files are not compressed using the zlib algorithm, and the format of the compressed data is very different from that of gzip.
Therefore, you should choose different compression techniques according to your specific needs: if you only need to compress/decompress the data, you can directly implement it with zlib. If you need to generate gzip format files or decompress the compression results of other tools, you must use gzip or zip and other related classes to handle it.
maven dependency
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.12</version> </dependency>
zip format
public static void zip(String input, String output, String name) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(output)); String[] paths = input.split("//|"); File[] files = new File[paths.length]; byte[] buffer = new byte[1024]; for (int i = 0; i < paths.length; i++) { files[i] = new File(paths[i]); } for (int i = 0; i < files.length; i++) { FileInputStream fis = new FileInputStream(files[i]); if (files.length == 1 && name != null) { out.putNextEntry(new ZipEntry(name)); } else { out.putNextEntry(new ZipEntry(files[i].getName())); } int len; // Read the content of the file you need to download and package it into the zip file while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); fis.close(); } out.close(); } out.close(); } gzip package
public static void gzip(String input, String output, String name) throws Exception { String compress_name = null; if (name != null) { compress_name = name; } else { compress_name = new File(input).getName(); } byte[] buffer = new byte[1024]; try { GzipParameters gp = new GzipParameters(); //Set the file name in the compressed file gp.setFilename(compress_name); GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new FileOutputStream(output), gp); FileInputStream fis = new FileInputStream(input); int length; while ((length = fis.read(buffer)) > 0) { gcos.write(buffer, 0, length); } fis.close(); gcos.finish(); } catch (IOException ioe) { ioe.printStackTrace(); } } 7z pack
public static void z7z(String input, String output, String name) throws Exception { try { SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(output)); SevenZArchiveEntry entry = null; String[] paths = input.split("//|"); File[] files = new File[paths.length]; for (int i = 0; i < paths.length; i++) { files[i] = new File(paths[i].trim()); } for (int i = 0; i < files.length; i++) { BufferedInputStream input = null; enter = new BufferedInputStream(new FileInputStream(paths[i])); if (name != null) { entry = sevenZOutput.createArchiveEntry(new File(paths[i]), name); } else { entry = sevenZOutput.createArchiveEntry(new File(paths[i]), new File(paths[i]).getName()); } sevenZOutput.putArchiveEntry(entry); byte[] buffer = new byte[1024]; int len; while ((len = enterstream.read(buffer)) > 0) { sevenZOutput.write(buffer, 0, len); } enter.close(); sevenZOutput.closeArchiveEntry(); } sevenZOutput.close(); } sevenZOutput.close(); } catch (IOException ioe) { System.out.println(ioe.toString() + " " + input); } } zlib package
public static void zlib(String input, String output) throws Exception {// DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(output)); DeflateParameters dp = new DeflateParameters(); dp.setWithZlibHeader(true); DeflateCompressorOutputStream dcos = new DeflateCompressorOutputStream(new FileOutputStream(output),dp); FileInputStream fis = new FileInputStream(input); int length = (int) new File(input).length(); byte data[] = new byte[length]; // int length; while ((length = fis.read(data)) > 0) { dcos.write(data, 0, length); } fis.close(); dcos.finish(); dcos.close(); }I hope this article will be helpful to you. This is all for you to introduce the compressed and packaged contents of Java implementation of zip, gzip, 7z, and zlib formats. I hope everyone will continue to follow our website! If you want to learn Java, you can continue to follow this website.