View Javadoc

1   package org.apache.maven.index.util.zip;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  public class ZipFacade
26  {
27      public static final long MEGABYTE = 1048576L;
28  
29      public static final long JAVA_ZIPFILE_SIZE_THRESHOLD = Long.getLong(
30          "org.apache.maven.index.util.zip.ZipFacade.javaZipFileSizeThreshold", 100L * MEGABYTE );
31  
32      private static final boolean TRUEZIP_AVAILABLE;
33  
34      static
35      {
36          Class<?> clazz;
37  
38          try
39          {
40              clazz = Class.forName( "de.schlichtherle.truezip.zip.ZipFile" );
41          }
42          catch ( ClassNotFoundException e )
43          {
44              clazz = null;
45          }
46  
47          TRUEZIP_AVAILABLE = clazz != null;
48      }
49  
50      public static ZipHandle getZipHandle( File targetFile )
51          throws IOException
52      {
53          if ( targetFile.isFile() )
54          {
55              if ( TRUEZIP_AVAILABLE && targetFile.length() > JAVA_ZIPFILE_SIZE_THRESHOLD )
56              {
57                  return new TrueZipZipFileHandle( targetFile );
58              }
59              else
60              {
61                  return new JavaZipFileHandle( targetFile );
62              }
63          }
64  
65          throw new IOException( "The targetFile should point to an existing ZIP file!" );
66      }
67  
68      public static void close( ZipHandle handle )
69          throws IOException
70      {
71          if ( handle != null )
72          {
73              handle.close();
74          }
75      }
76  }