Java ZipOutputStream Create getOutputStream(File tarFile)

Here you can find the source of getOutputStream(File tarFile)

Description

If tarFile's extension is simply tar, then returns a new FileOutputStream.

License

Open Source License

Exception

Parameter Description
IllegalArgumentException if tarFile has an unrecognized extension
IOException if an I/O problem occurs

Declaration

private static OutputStream getOutputStream(File tarFile) throws IllegalArgumentException, IOException 

Method Source Code


//package com.java2s;
/*/*w  ww .  j av a  2s.  c  om*/
Copyright ? 2008 Brent Boyer
    
This program 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 3 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 Lesser GNU General Public License for more details.
    
You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**
    * If tarFile's extension is simply tar, then returns a new FileOutputStream.
    * Else if tarFile's extension is tar.gz or tgz, then returns a new GZIPOutputStream wrapping a new FileOutputStream.
    * <p>
    * Note: the result is never buffered, since the TarArchiveOutputStream which will use the result always has an internal buffer.
    * <p>
    * @throws IllegalArgumentException if tarFile has an unrecognized extension
    * @throws IOException if an I/O problem occurs
    * @see <a href="http://www.gzip.org/">gzip home page</a>
    * @see <a href="http://www.brouhaha.com/~eric/tgz.html">.tar.gz file format FAQ</a>
    */
    private static OutputStream getOutputStream(File tarFile) throws IllegalArgumentException, IOException {
        String name = tarFile.getName().toLowerCase();
        if (name.endsWith(".tar")) {
            return new FileOutputStream(tarFile);
        } else if (name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
            return new GZIPOutputStream(new FileOutputStream(tarFile));
        } else {
            throw new IllegalArgumentException(
                    "tarFile = " + tarFile.getPath() + " has an invalid extension for a tar or tar/gzip file");
        }
    }
}

Related

  1. getOutputStream(File file)
  2. getOutputStream(File file, boolean append)
  3. getOutputStream(String outputFileName)