1 package com.atlassian.core.util.zip;
2
3 import com.google.common.annotations.VisibleForTesting;
4 import org.apache.commons.lang.StringUtils;
5
6 import java.io.File;
7 import java.io.IOException;
8
9 public class FolderArchiver
10 {
11 //~ Instance variables ---------------------------------------------------------------------------------------------
12
13 private final File folderToArchive;
14 private final File archiveFile;
15
16 //~ Constructors ---------------------------------------------------------------------------------------------------
17 public FolderArchiver(final File folderToArchive, final File archiveFile)
18 {
19 this.folderToArchive = folderToArchive;
20 this.archiveFile = archiveFile;
21 }
22
23 //~ Methods --------------------------------------------------------------------------------------------------------
24
25 /**
26 * Adds all entries from a folder to an archive
27 *
28 * @throws IOException if folder does not exist, can't read from directory or can't create an archive
29 */
30 public void doArchive() throws IOException
31 {
32 doFolderArchive("");
33 }
34
35 /**
36 * Adds all entries from a folder to an archive
37 *
38 * @param archiveFolderToCreate name of topmost folder to create inside of the archive, leave blank to not create.
39 * @throws IOException if folder does not exist, can't read from directory or can't create an archive
40 */
41 @VisibleForTesting
42 void doFolderArchive(final String archiveFolderToCreate) throws IOException
43 {
44 final ZipArchiver zipArchiver = new ZipArchiver(archiveFile);
45 try
46 {
47 final String cleanedArchiveFolder = StringUtils.stripToEmpty(FilePathUtils.stripSlashes(archiveFolderToCreate));
48 zipArchiver.addFolder(folderToArchive, cleanedArchiveFolder);
49 }
50 finally
51 {
52 zipArchiver.close();
53 }
54 }
55 }