Java Zip String zipStringAsFile(String contents, File destZipFile, String internalFilename, String comment)

Here you can find the source of zipStringAsFile(String contents, File destZipFile, String internalFilename, String comment)

Description

zip String As File

License

Open Source License

Declaration

public static void zipStringAsFile(String contents, File destZipFile, String internalFilename, String comment)
            throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**//from   ww  w  .j  av a 2s. c o m
 *  neuroConstruct
 *  Software for developing large scale 3D networks of biologically realistic neurons
 * 
 *  Copyright (c) 2009 Padraig Gleeson
 *  UCL Department of Neuroscience, Physiology and Pharmacology
 *
 *  Development of this software was made possible with funding from the
 *  Medical Research Council and the Wellcome Trust
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program 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.  See the
 *  GNU General Public License for more details.
    
 *  You should have received a copy of the GNU General Public License
 *  along with this program; 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.*;

public class Main {
    public static void zipStringAsFile(String contents, File destZipFile, String internalFilename, String comment)
            throws FileNotFoundException, IOException {
        int BUFFER = 2048;
        byte data[] = new byte[BUFFER];
        BufferedInputStream origin = null;

        FileOutputStream dest = new FileOutputStream(destZipFile);

        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));

        ByteArrayInputStream fi = new ByteArrayInputStream(contents.getBytes());
        origin = new BufferedInputStream(fi, BUFFER);

        ZipEntry entry = new ZipEntry(internalFilename);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();

        if (comment != null)
            out.setComment(comment);

        out.close();

    }
}

Related

  1. zip(String str)
  2. zip(String toCompress)
  3. zip_Str(String in_str)
  4. zipString(String input)
  5. zipString(String str)
  6. zipStringToBytes(String input)
  7. zipText(String text)