View Javadoc

1   /*
2    * Copyright (c) 2003 by Atlassian Software Systems Pty. Ltd.
3    * All rights reserved.
4    */
5   package com.atlassian.plugin.util.zip;
6   
7   import org.apache.commons.lang.StringUtils;
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  import java.io.*;
12  import java.util.zip.ZipEntry;
13  
14  public class FileUnzipper extends AbstractUnzipper
15  {
16      private static final Log log = LogFactory.getLog(FileUnzipper.class);
17  
18      private File zipFile;
19      private File destDir;
20  
21      public FileUnzipper(File zipFile, File destDir)
22      {
23          this.zipFile = zipFile;
24          this.destDir = destDir;
25      }
26  
27      /**
28       * Unzips all files in the archive
29       *
30       * @throws Exception
31       */
32      public void unzip() throws IOException
33      {
34          if ((zipFile == null) || !zipFile.isFile())
35              return;
36  
37          getStreamUnzipper().unzip();
38      }
39  
40      public ZipEntry[] entries() throws IOException
41      {
42          return getStreamUnzipper().entries();
43      }
44  
45      /**
46       * Specify a specific file inside the archive to extract
47       *
48       * @param fileName
49       */
50      public File unzipFileInArchive(String fileName) throws IOException
51      {
52          File result = null;
53  
54          if ((zipFile == null) || !zipFile.isFile() || !StringUtils.isNotEmpty(fileName))
55              return result;
56  
57          result = getStreamUnzipper().unzipFileInArchive(fileName);
58  
59          if (result == null)
60              log.error("The file: " + fileName + " could not be found in the archive: " + zipFile.getAbsolutePath());
61  
62          return result;
63      }
64  
65      private StreamUnzipper getStreamUnzipper() throws FileNotFoundException
66      {
67          return new StreamUnzipper(new BufferedInputStream(new FileInputStream(zipFile)), destDir);
68      }
69  
70  }