Java Utililty Methods Zip Folder

List of utility methods to do Zip Folder

Description

The list of methods to do Zip Folder are organized into topic(s).

Method

FilezipFolder(File root)
zip Folder
if (!root.isDirectory())
    throw new IOException("Zip root must be a folder");
File zipFile = File.createTempFile("module-doc", ".zip");
try {
    ZipOutputStream os = new ZipOutputStream(new FileOutputStream(zipFile));
    try {
        for (File f : root.listFiles())
            zipInternal("", f, os);
...
FilezipFolder(File root)
zip Folder
if (root == null)
    return null;
if (!root.isDirectory())
    return null;
File out = File.createTempFile("SHA", null);
ZipOutputStream os = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(out)));
addFolder("", root, os);
os.flush();
...
voidzipFolder(File source, File destination)
This method allows you to zip a folder.
if (!source.isDirectory()) {
    throw new IOException("Source must be folder.");
if (destination.exists()) {
    throw new IOException("Destination already exists.");
ZipOutputStream zipOutputStream = new ZipOutputStream(
        new BufferedOutputStream(new FileOutputStream(destination)));
...
voidzipFolder(File srcFolder, File destZipFile, boolean zipOnlySrcFolderContentsAndNotSrcFolder)
zip Folder
destZipFile.getParentFile().mkdirs();
FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter);
if (zipOnlySrcFolderContentsAndNotSrcFolder) {
    for (File file : srcFolder.listFiles()) {
        addFileToZip("", file, zip, false);
} else {
...
FilezipFolder(File srcFolder, String destZipFile)
zip Folder
final File file = new File(srcFolder, destZipFile);
final ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
try {
    addFolderToZip("", srcFolder, zip, destZipFile);
} finally {
    zip.close();
return file;
...
voidzipFolder(final File directory, final String path, final ZipOutputStream out)
zip Folder
if (!"".equals(path)) {
    out.putNextEntry(new ZipEntry(path));
final File[] filesToAdd = directory.listFiles(new FileFilter() {
    @Override
    public boolean accept(final File file) {
        return file.isFile();
});
if (filesToAdd != null) {
    final byte data[] = new byte[DEFAULT_BUFFER_SIZE];
    BufferedInputStream origin = null;
    for (final File f : filesToAdd) {
        out.putNextEntry(new ZipEntry(path + f.getName()));
        final FileInputStream fi = new FileInputStream(f);
        origin = new BufferedInputStream(fi, DEFAULT_BUFFER_SIZE);
        int count;
        while ((count = origin.read(data, 0, DEFAULT_BUFFER_SIZE)) != -1) {
            out.write(data, 0, count);
        origin.close();
final File[] directoriesToAdd = directory.listFiles(new FileFilter() {
    @Override
    public boolean accept(final File file) {
        return file.isDirectory();
});
if (directoriesToAdd != null) {
    for (final File dir : directoriesToAdd) {
        zipFolder(dir, path + File.separator + dir.getName() + File.separator, out);
voidzipFolder(final File srcFolder, final OutputStream out)
zip Folder
ZipOutputStream outZip = null;
try {
    outZip = new ZipOutputStream(out);
    addFolderToZip("", srcFolder, outZip);
} finally {
    if (outZip != null) {
        try {
            outZip.close();
...
voidzipFolder(final String srcFolder, final String destZipFile)
Zip folder.
try {
    final FileOutputStream fileWriter = new FileOutputStream(destZipFile);
    final ZipOutputStream zip = new ZipOutputStream(fileWriter);
    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
} catch (Exception e) {
    e.printStackTrace();
...
voidzipFolder(String sourceDir, String destDir, String name)
zip Folder
FileOutputStream fos = new FileOutputStream(destDir + "/" + name + ".zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(sourceDir);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
voidzipFolder(String sourceFolder, String target)
zip Folder
List<String> fileList = generateFileList(sourceFolder, new File(sourceFolder));
byte[] buffer = new byte[1024];
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
    fos = new FileOutputStream(target);
    zos = new ZipOutputStream(fos);
    FileInputStream in = null;
...