Java ZipOutputStream Write zipInternal(String path, File file, ZipOutputStream os, boolean justFolders, boolean skipManifest)

Here you can find the source of zipInternal(String path, File file, ZipOutputStream os, boolean justFolders, boolean skipManifest)

Description

zip Internal

License

Apache License

Declaration

private static void zipInternal(String path, File file, ZipOutputStream os, boolean justFolders,
            boolean skipManifest) throws IOException 

Method Source Code

//package com.java2s;
/********************************************************************************
 * Copyright (c) 2011-2017 Red Hat Inc. and/or its affiliates and others
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Apache License, Version 2.0 which is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * SPDX-License-Identifier: Apache-2.0 // ww  w  .ja va2s  .  co  m
 ********************************************************************************/

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static void zipInternal(String path, File file, ZipOutputStream os, boolean justFolders,
            boolean skipManifest) throws IOException {
        String filePath;
        if (path.isEmpty())
            filePath = file.getName();
        else
            filePath = path + "/" + file.getName();
        if (file.isDirectory()) {
            if (justFolders) {
                if (!filePath.equalsIgnoreCase("META-INF") || !skipManifest) {
                    ZipEntry entry = new ZipEntry(filePath + "/");
                    os.putNextEntry(entry);
                    os.closeEntry();
                }
            }
            for (File f : file.listFiles())
                zipInternal(filePath, f, os, justFolders, skipManifest);
        } else if (!justFolders) {
            if (!filePath.equalsIgnoreCase("META-INF/MANIFEST.MF") || !skipManifest) {
                ZipEntry entry = new ZipEntry(filePath);
                os.putNextEntry(entry);
                try (FileInputStream in = new FileInputStream(file)) {
                    copyStreamNoClose(in, os);
                }
                os.closeEntry();
            }
        }
    }

    private static void copyStreamNoClose(InputStream in, OutputStream out) throws IOException {
        final byte[] bytes = new byte[8192];
        int cnt;
        while ((cnt = in.read(bytes)) != -1) {
            out.write(bytes, 0, cnt);
        }
        out.flush();
    }
}

Related

  1. zipFile(ZipOutputStream zipOut, String path, File file)
  2. zipFile(ZipOutputStream zos, BufferedOutputStream out, File destPath, String originalPath)
  3. zipFile(ZipOutputStream zos, File file, String requestName, byte[] buf)
  4. zipFile(ZipOutputStream zout, File file, int rootLength)
  5. zipInternal(String path, File file, ZipOutputStream os)
  6. zipIt(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc)
  7. zipOneFile(File file, String base, ZipOutputStream zout)
  8. zipOneFile(File file, ZipOutputStream out, String relativePath)
  9. zipOneFile(String prefix, File file, ZipOutputStream zos)