| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 42 (42) |
Complexity: 12 |
Complexity Density: 0.44 |
|
| 10 |
|
public class FolderArchiver |
| 11 |
|
{ |
| 12 |
|
|
| 13 |
|
|
| 14 |
|
private static final int BUFFER_SIZE = 10240; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
private File folderToArchive; |
| 19 |
|
private File archiveFile; |
| 20 |
|
|
| 21 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 22 |
0
|
public FolderArchiver(File folderToArchive, File archiveFile)... |
| 23 |
|
{ |
| 24 |
0
|
this.folderToArchive = folderToArchive; |
| 25 |
0
|
this.archiveFile = archiveFile; |
| 26 |
|
} |
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 30 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 32 (32) |
Complexity: 10 |
Complexity Density: 0.5 |
|
| 40 |
0
|
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 |
|
|
| 54 |
0
|
path = path.replaceAll("\\\\", "/"); |
| 55 |
|
|
| 56 |
|
|
| 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 |
|
} |