Java Zip File zip(InputStream is, String dataFileName, File zipFile)

Here you can find the source of zip(InputStream is, String dataFileName, File zipFile)

Description

Zip the given stream content into the given File

License

Open Source License

Parameter

Parameter Description
is Input stream providing the content to be zipped
dataFileName The name of the file that will be encompass within thw Zip file having the content
zipFile Zip file with path to which the Zip file will be written

Exception

Parameter Description
IOException an exception

Declaration

public static void zip(InputStream is, String dataFileName, File zipFile) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;
import java.util.zip.*;

public class Main {
    /**/*from   ww w . ja v  a2s. c o  m*/
     * Zip the given stream content into the given File
     *
     * @param is Input stream providing the content to be zipped
     * @param dataFileName The name of the file that will be encompass within
     * thw Zip file having the content
     * @param zipFile Zip file with path to which the Zip file will be written
     *
     * @throws IOException
     */
    public static void zip(InputStream is, String dataFileName, File zipFile) throws IOException {

        // Create the Zip output stream
        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zipout = new ZipOutputStream(fos);

        // Create an entry (ie the content file name) for the Zip file
        ZipEntry zent = new ZipEntry(dataFileName);
        zipout.putNextEntry(zent);

        // Write the given data to the Zip output stream
        byte[] b = new byte[1024]; // Read 1K at a time
        int bytesRead = 0; // Number of bytes read by "read(byte[])" call
        while ((bytesRead = is.read(b)) != -1) {
            zipout.write(b, 0, bytesRead);
        }

        // Close the entry and the output streams
        zipout.closeEntry();
        zipout.close();
        fos.close();
        is.close();
    }

    /**
     * Zip the given File. This will zip the file in the dir where the source
     * file is and the zip file name will be the source file name minus any last
     * '.extention' plus ".zip".
     *
     * @param file Given file
     *
     * @throws IOException
     */
    public static void zip(File file) throws IOException {
        // Check the input
        if (file == null || !file.exists()) {
            throw new IOException("File " + file.getName() + " does not exist.");
        }

        // Format the inputs required for zip.
        // Generate the zip file name (fileNameWithoutExt.zip)
        FileInputStream fis = new FileInputStream(file);
        String dataFileName = file.getName();
        int idx = dataFileName.lastIndexOf(".");
        String zipFileName = (idx == -1) ? dataFileName + ".zip" : dataFileName.substring(0, idx) + ".zip";
        String zipFileNameWithPath = file.getParentFile().getPath() + System.getProperty("file.separator")
                + zipFileName;
        File zipFile = new File(zipFileNameWithPath);

        // Zip
        zip(fis, dataFileName, zipFile);
    }

    /**
     * Zip the given string content into the given File
     *
     * @param data Text string (to zip)
     * @param dataFileName The name of the file that will be encompass within
     * thw Zip file having the content
     * @param zipFile Zip file path
     *
     * @throws IOException
     */
    public static void zip(String data, String dataFileName, File zipFile) throws IOException {

        // Convert the String into the ByteArray stream
        ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes());

        // Zip
        zip(bais, dataFileName, zipFile);
    }

    /**
     * Zip the given source File into the given destination file.
     *
     * @param sourceFile Given source file to be zipped
     * @param destinationFile Zip file to be written.
     * @throws IOException
     */
    public static void zip(File sourceFile, File destinationFile) throws IOException {

        zip(new FileInputStream(sourceFile), sourceFile.getName(), destinationFile);
    }
}

Related

  1. zip(final File[] filesToZip, final File zipFile)
  2. zip(final Map files)
  3. zip(final String fileName, final byte[] fileContent)
  4. zip(final String sourceFileDir, final String zipFile)
  5. zip(InputStream is)
  6. zip(OutputStream outputStream, File targetFile)
  7. zip(String data, String fileName)
  8. zip(String dir, String destFile)
  9. zip(String filesDirToBeZipped, String destFileName, String manifest)