Java Zip File zip(String data, String fileName)

Here you can find the source of zip(String data, String fileName)

Description

Zips the specified Data String.

License

Open Source License

Parameter

Parameter Description
data Data String.

Exception

Parameter Description
IOException Input Output Exception.

Return

Array of bytes.

Declaration

public static byte[] zip(String data, String fileName) throws IOException 

Method Source Code

//package com.java2s;
/** Copyright (c) 2006 Memorial Sloan-Kettering Cancer Center.
 **//from  w ww . ja  v  a  2s.  c  o  m
 ** Code written by: Ethan Cerami
 ** Authors: Ethan Cerami, Gary Bader, Chris Sander
 **
 ** This library is free software; you can redistribute it and/or modify it
 ** under the terms of the GNU Lesser General Public License as published
 ** by the Free Software Foundation; either version 2.1 of the License, or
 ** any later version.
 **
 ** This library is distributed in the hope that it will be useful, but
 ** WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
 ** MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
 ** documentation provided hereunder is on an "as is" basis, and
 ** Memorial Sloan-Kettering Cancer Center 
 ** has no obligations to provide maintenance, support,
 ** updates, enhancements or modifications.  In no event shall
 ** Memorial Sloan-Kettering Cancer Center
 ** be liable to any party for direct, indirect, special,
 ** incidental or consequential damages, including lost profits, arising
 ** out of the use of this software and its documentation, even if
 ** Memorial Sloan-Kettering Cancer Center 
 ** has been advised of the possibility of such damage.  See
 ** the GNU Lesser General Public License for more details.
 **
 ** You should have received a copy of the GNU Lesser General Public License
 ** along with this library; if not, write to the Free Software Foundation,
 ** Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 **/

import java.io.*;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * Zips the specified Data String.
     *
     * @param data Data String.
     * @return Array of bytes.
     * @throws IOException Input Output Exception.
     */
    public static byte[] zip(String data, String fileName) throws IOException {
        byte bytes[] = data.getBytes();
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(byteStream);
        ZipEntry entry = new ZipEntry(fileName);
        zipOut.putNextEntry(entry);
        zipOut.write(bytes, 0, bytes.length);
        zipOut.closeEntry();
        byte zipData[] = byteStream.toByteArray();
        zipOut.close();
        return zipData;
    }
}

Related

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