Для этого вы можете использовать Zeroturnaround Zip .
ZipUtil.pack(new File("/your/folder/here"), new File("/your/zip/here.zip"));
Добавление этой зависимости
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-zip</artifactId>
<version>1.13</version>
<type>jar</type>
</dependency>
[ОБНОВЛЕНО]
Этоспособ сделать это с помощью решения Java 8:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class AppZip {
private List<String> fileList;
private String sourceFolder;
AppZip(String sourceFolder) {
this.sourceFolder = sourceFolder;
fileList = new ArrayList<>();
}
/**
* Zip it
*
* @param zipFile output ZIP file location
*/
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
try {
generateFileList(new File(sourceFolder));
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String file : this.fileList) {
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream fin =
new FileInputStream(sourceFolder + File.separator + file);
int len;
while ((len = fin.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
fin.close();
}
zos.closeEntry();
zos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Traverse a directory and get all files,
* and add the file into fileList
*
* @param node file or directory
*/
public void generateFileList(File node) {
//add file only
if (node.isFile()) {
fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
}
if (node.isDirectory()) {
String[] subNote = node.list();
for (String filename : subNote) {
generateFileList(new File(node, filename));
}
}
}
/**
* Format the file path for zip
*
* @param file file path
* @return Formatted file path
*/
private String generateZipEntry(String file) {
return file.substring(sourceFolder.length() + 1);
}
}
И затем вызвать:
AppZip appZip = new AppZip("/your/folder/here");
appZip.zipIt("/your/zip/here");