Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: PackUtils.java 14720 2011-09-15 15:17:42Z tchemit $
   *
   * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
   * Santa Clara, California 95054, U.S.A. All rights reserved.
   *
   * 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 (at your option) 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.  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 package org.jdesktop.deployment.ant.pack200;
 
 import  org.apache.tools.ant.util.FileUtils;
 
 import java.io.*;
 import java.util.Map;
Utility methods for packing and unpacking jar files.

Author(s):
Emmanuel Bourg
Version:
$Revision: 14720 $, $Date: 2011-09-15 17:17:42 +0200 (Thu, 15 Sep 2011) $
 
 public class PackUtils
 {

    
Pack a jar.

Parameters:
source the source jar
destination the packed jar
props the packing properties
gzip true if the destination file
 
     public static void packFile sourceFile destinationMap propsboolean gzip )
         throws IOException
     {
         JarFile jar = null;
         OutputStream out = null;
         try
         {
             out = new FileOutputStreamdestination );
             if ( gzip )
             {
                 out = new GZIPOutputStreamout )
                 {
                     {
                         .setLevel. );
                     }
                 };
             }
             out = new BufferedOutputStreamout );
 
             jar = new JarFilesourcefalse );
 
             Pack200.Packer packer = Pack200.newPacker();
             packer.properties().putAllprops );
             packer.packjarout );
         }
         finally
         {
             FileUtils.close( out );
             if ( jar != null )
             {
                 jar.close();
             }
         }
     }

    
Repack a jar.

Parameters:
source the source jar
destination the destination jar (may be the same as the source jar)
props the packing properties
 
     public static void repackFile sourceFile destinationMap props )
         throws IOException
     {
         File tempFile = new Filesource.toString() + ".tmp" );
 
         try
        {
            packsourcetempFilepropsfalse );
            unpacktempFiledestinationprops );
        }
        finally
        {
            tempFile.delete();
        }
    }

    
Unpack a jar.

Parameters:
source the packed jar
destination the unpacked jar
props the packing properties
    public static void unpackFile sourceFile destinationMap props )
        throws IOException
    {
        InputStream in = null;
        JarOutputStream out = null;
        try
        {
            in = new FileInputStreamsource );
            if ( isGzippedsource ) )
            {
                in = new GZIPInputStreamin );
            }
            in = new BufferedInputStreamin );
            out = new JarOutputStreamnew BufferedOutputStreamnew FileOutputStreamdestination ) ) );
            Pack200.Unpacker unpacker = Pack200.newUnpacker();
            unpacker.properties().putAllprops );
            unpacker.unpackinout );
        }
        finally
        {
            FileUtils.close( in );
            FileUtils.close( out );
        }
    }

    
Tells if the specified file is gzipped.

Parameters:
file
    private static boolean isGzippedFile file )
        throws IOException
    {
        DataInputStream is = new DataInputStreamnew FileInputStreamfile ) );
        int i = is.readInt();
        is.close();
        return ( i & 0xffffff00 ) == 0x1f8b0800;
    }
New to GrepCode? Check out our FAQ X