View Javadoc

1   package com.atlassian.core.util.zip;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.IOException;
7   import java.util.zip.ZipEntry;
8   import java.util.zip.ZipOutputStream;
9   
10  public class FolderArchiver
11  {
12      //~ Static variables/initializers ----------------------------------------------------------------------------------
13  
14      private static final int BUFFER_SIZE = 10240;
15  
16      //~ Instance variables ---------------------------------------------------------------------------------------------
17  
18      private File folderToArchive;
19      private File archiveFile;
20  
21      //~ Constructors ---------------------------------------------------------------------------------------------------
22      public FolderArchiver(File folderToArchive, File archiveFile)
23      {
24          this.folderToArchive = folderToArchive;
25          this.archiveFile = archiveFile;
26      }
27  
28      //~ Methods --------------------------------------------------------------------------------------------------------
29  
30      public void doArchive() throws Exception
31      {
32          FileOutputStream stream = new FileOutputStream(archiveFile);
33          ZipOutputStream output = new ZipOutputStream(stream);
34  
35          compressFile(folderToArchive, output);
36          output.close();
37          stream.close();
38      }
39  
40      private void compressFile(File file, ZipOutputStream output)
41          throws IOException
42      {
43          if ((file == null) || !file.exists())
44          {
45              return;
46          }
47  
48          if (file.isFile() && !file.equals(archiveFile))
49          {
50              byte[] buffer = new byte[BUFFER_SIZE];
51              String path = file.getPath().substring(folderToArchive.getPath().length());
52  
53              // if creating zip on a windows system, convert all backslashes to forward slashes so that the resulting file is platform-independent (work on unix and windows)
54              path = path.replaceAll("\\\\", "/");
55  
56              // also drop the leading slash
57              if (path.length() > 0 && path.charAt(0) == '/')
58                  path = path.substring(1);
59  
60              ZipEntry entry = new ZipEntry(path);
61  
62              entry.setTime(file.lastModified());
63              output.putNextEntry(entry);
64  
65              FileInputStream in = new FileInputStream(file);
66              int data;
67  
68              while ((data = in.read(buffer, 0, buffer.length)) > 0)
69              {
70                  output.write(buffer, 0, data);
71              }
72  
73              in.close();
74          }
75          else if (file.isDirectory())
76          {
77              File[] files = file.listFiles();
78  
79              for (int i = 0; i < files.length; i++)
80              {
81                  compressFile(files[i], output);
82              }
83          }
84      }
85  }