Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
27   85   12   9
12   59   0.44   3
3     4  
1    
 
 
  FolderArchiver       Line # 10 27 12 0% 0.0
 
No Tests
 
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  0 toggle public FolderArchiver(File folderToArchive, File archiveFile)
23    {
24  0 this.folderToArchive = folderToArchive;
25  0 this.archiveFile = archiveFile;
26    }
27   
28    //~ Methods --------------------------------------------------------------------------------------------------------
29   
 
30  0 toggle public void doArchive() throws Exception
31    {
32  0 FileOutputStream stream = new FileOutputStream(archiveFile);
33  0 ZipOutputStream output = new ZipOutputStream(stream);
34   
35  0 compressFile(folderToArchive, output);
36  0 output.close();
37  0 stream.close();
38    }
39   
 
40  0 toggle private void compressFile(File file, ZipOutputStream output)
41    throws IOException
42    {
43  0 if ((file == null) || !file.exists())
44    {
45  0 return;
46    }
47   
48  0 if (file.isFile() && !file.equals(archiveFile))
49    {
50  0 byte[] buffer = new byte[BUFFER_SIZE];
51  0 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  0 path = path.replaceAll("\\\\", "/");
55   
56    // also drop the leading slash
57  0 if (path.length() > 0 && path.charAt(0) == '/')
58  0 path = path.substring(1);
59   
60  0 ZipEntry entry = new ZipEntry(path);
61   
62  0 entry.setTime(file.lastModified());
63  0 output.putNextEntry(entry);
64   
65  0 FileInputStream in = new FileInputStream(file);
66  0 int data;
67   
68  0 while ((data = in.read(buffer, 0, buffer.length)) > 0)
69    {
70  0 output.write(buffer, 0, data);
71    }
72   
73  0 in.close();
74    }
75  0 else if (file.isDirectory())
76    {
77  0 File[] files = file.listFiles();
78   
79  0 for (int i = 0; i < files.length; i++)
80    {
81  0 compressFile(files[i], output);
82    }
83    }
84    }
85    }