Java GZip File Create newGzipFile(String content, String name)

Here you can find the source of newGzipFile(String content, String name)

Description

Creates a temporary GzipFile withe the content at content .

License

Apache License

Parameter

Parameter Description
content a parameter
name how the temporal file will be called

Exception

Parameter Description
IOException an exception

Declaration

public static File newGzipFile(String content, String name) throws IOException 

Method Source Code


//package com.java2s;
/*//from  ww  w.  j a v a 2s .  c om
 * Copyright 2016-2017 EMBL - European Bioinformatics Institute
 *
 * 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.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.io.Writer;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**
     * Creates a temporary GzipFile withe the content at {@param content}.
     * @param content
     * @param name how the temporal file will be called
     * @return
     * @throws IOException
     */
    public static File newGzipFile(String content, String name) throws IOException {
        File tempFile = File.createTempFile(name, ".gz");
        try (FileOutputStream output = new FileOutputStream(tempFile)) {
            try (Writer writer = new OutputStreamWriter(new GZIPOutputStream(output), "UTF-8")) {
                writer.write(content);
            }
        }
        return tempFile;
    }
}