If you want to compress a folder and all its subfolders and subfolders in Java, you can take advantage of the ant plug-ins provided by Apache Corporation. It's actually a jar package.
For example, if you want to compress all the files under F:\bb into a f:\bb.zip, such as:
First go to Apache's official website, download the ant plugin, Address: http://ant.apache.org/bindownload.cgi (click to open the link)
After downloading the extract, take the apache-ant-1.9.4\lib under the Ant.jar and put it in your Java project:
For example, copy to your Java project, directory structure like, in Eclipse right-click your Java project Properties->java build path->add Jars Select the Ant.jar you just copied over:
After that, first introduce the following package at the beginning:
Package Ziptest;import Java.io.*;import Java.util.*;import org.apache.tools.zip.*;
Then, like "Java" read all the folders and files under the path "(Click Open link), read the folder you want to compress all the file path, with a arraylist<string> save, and then as" Java "arraylist< string> conversion to string array problem (click Open link) to convert it to a string array
public static void Main (string[] args) throws Exception {//Read the path of all files under f:/bb arraylist<string> Dirstrarr = new Array List<string> (); File dir = new file ("F:/bb"), if (Dir.exists ()) {File files[] = Dir.listfiles (), for (File file:files) {Dirstrarr.add (dir. GetPath () + "/" + file.getname ());}} The output is F:/bb.zip, followed by a request to read all the files compress (new FileOutputStream ("F:/bb.zip"), (string[]) Dirstrarr.toarray (new string[ 0]));}
Then use the following Java method to compress it:
In the case of a folder, the loop recursively passes this method because it also reads all of its subfolders and sub-files. private static void Zipdirectory (Zipoutputstream zos, String dirname,string basepath) throws Exception {File dir = new Fil E (DirName), if (Dir.exists ()) {File files[] = Dir.listfiles (), if (Files.length > 0) {for (File file:files) {//Read In both cases, file, folder if (File.isdirectory ()) {zipdirectory (Zos,file.getpath (), basepath+ file.getname (). SUBSTRING ( File.getname (). LastIndexOf (File.separator) + 1) + file.separator);} Elsezipfile (Zos, File.getpath (), basepath);} If it is an empty folder} else {//Direct compression can be zipentry ze = new ZipEntry (basepath); Zos.putnextentry (Ze);}} private static void ZipFile (Zipoutputstream zos, String filename,string basepath) throws Exception {//If it is a file, the input/output stream should be used, Fragment Compressed File File = new file (filename); if (File.exists ()) {FileInputStream FIS = new FileInputStream (filename); ZipEntry ze = new ZipEntry (BasePath + file.getname ()); Zos.putnextentry (ze); byte[] buffer = new Byte[8192];int count = 0;wh Ile ((count = fis.read (buffer)) > 0) {zos.write (buffer, 0, Count);} Fis.close ();}} public static void Compress (OutputStream os, String ... paths) throws Exception {//Specifies that the output is sent over oszipoutputstream Zos = new Zipoutputstream (OS);//traversal of the path passed for (String path:paths) {//If the path passed in is empty, this loop is not performed to avoid an error if (Path.equals ("")) continue ; File File = new file (path);//after which, if this path exists, there are two cases, one is a folder, and the other is the file if (file.exists ()) {if (File.isdirectory ()) {Zipdirectory (Zos, File.getpath (), file.getname () + file.separator);} else {ZipFile (Zos, File.getpath (), "");}}} Zos.close ();}
The whole file is connected like this:
Package Ziptest;import java.io.*;import java.util.*;import org.apache.tools.zip.*;p ublic class ZipTest {//If it is a folder, This method is recursive because it also reads all of its subfolders and sub-files. private static void Zipdirectory (Zipoutputstream zos, String dirname,string basepath) throws Exception {File dir = new Fil E (DirName), if (Dir.exists ()) {File files[] = Dir.listfiles (), if (Files.length > 0) {for (File file:files) {//Read In two cases, file, folder if (File.isdirectory ()) {zipdirectory (Zos,file.getpath (), basepath+ file.getname (). SUBSTRING ( File.getname (). LastIndexOf (File.separator) + 1) + file.separator);} Elsezipfile (Zos, File.getpath (), basepath);} If it is an empty folder} else {//Direct compression can be zipentry ze = new ZipEntry (basepath); Zos.putnextentry (Ze);}} private static void ZipFile (Zipoutputstream zos, String filename,string basepath) throws Exception {//If it is a file, the input/output stream should be used, segmented Compress file File = new file (filename), if (File.exists ()) {FileInputStream FIS = new FileInputStream (filename); ZipEntry ze = new ZipEntry (BasePath + file.getname ()); Zos.putnextentry (ze); byte[] buffer = new Byte[8192];int count = 0;while ((count = fis.read (buffer)) > 0) {zos.write (buffer, 0, count);} Fis.close ();}} public static void Compress (OutputStream os, String ... paths) throws Exception {//Specifies that the output is sent over oszipoutputstream Zos = new Zipoutputstream (OS);//traversal of the path passed for (String path:paths) {//If the path passed in is empty, this loop is not performed to avoid an error if (Path.equals ("")) continue ; File File = new file (path);//after which, if this path exists, there are two cases, one is a folder, and the other is the file if (file.exists ()) {if (File.isdirectory ()) {Zipdirectory (Zos, File.getpath (), file.getname () + file.separator);} else {ZipFile (Zos, File.getpath (), "");}}} Zos.close ();} public static void Main (string[] args) throws Exception {//Read the path of all files under f:/bb arraylist<string> Dirstrarr = new Array List<string> (); File dir = new file ("F:/bb"), if (Dir.exists ()) {File files[] = Dir.listfiles (), for (File file:files) {Dirstrarr.add (dir. GetPath () + "/" + file.getname ());}} The output is f:/bb.zip and then requires that all files be read compress (new FileOutputStream ("F:/bb.zip"), (string[]) Dirstrarr.toarrAy (new string[0]));}}
Java uses the ant plugin to compress the folder and all its subfolders and subfolders