Java Zip File zipFile(File file)

Here you can find the source of zipFile(File file)

Description

zip File

License

Apache License

Declaration

private static void zipFile(File file) 

Method Source Code

//package com.java2s;
/*//w  ww .j av a2s . co m
 *    Copyright 2005 The Regents of the University of Michigan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static ZipOutputStream zos = null;
    private static String source = "", target = "";

    private static void zipFile(File file) {
        if (file.isDirectory()) {
            if (file.getName().equalsIgnoreCase(".metadata") || file.getName().equalsIgnoreCase("Libraries")
                    || file.getName().equalsIgnoreCase("RepositoryBuild")
                    || file.getName().equalsIgnoreCase("images")) {
                return;
            }
            File list[] = file.listFiles();
            for (int i = 0; i < list.length; i++) {
                zipFile(list[i]);
            }
        } else {
            try {
                String strAbsPath = file.getPath();
                String strZipEntryName = strAbsPath.substring(source.length() + 1, strAbsPath.length());
                byte[] b = new byte[(int) (file.length())];
                ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
                zos.putNextEntry(cpZipEntry);
                zos.write(b, 0, (int) file.length());
                zos.closeEntry();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. zip(String zipFileName, String inputFile)
  2. zip(String zipFileName, String[] zipEntries)
  3. zip(String zipName, String[] fileNames, boolean path)
  4. zip(String zipPath, Map input)
  5. zipFile(File aFileToZip)
  6. zipFile(File file, File output)
  7. zipFile(File file, File zipFile)
  8. zipFile(File file, String zipFile)
  9. zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)