| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.geotoolkit.maven.jar; |
| 19 | |
|
| 20 | |
import java.io.*; |
| 21 | |
import java.util.Arrays; |
| 22 | |
import java.util.Comparator; |
| 23 | |
import java.util.zip.ZipEntry; |
| 24 | |
import java.util.zip.ZipOutputStream; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 0 | final class PackFiles implements FileFilter, Comparator<File> { |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private final File targetDirectory; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private String sourcePath; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
private ZipOutputStream out; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | private final byte[] buffer = new byte[8 * 1024]; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 0 | public PackFiles(final File targetDirectory) { |
| 62 | 0 | this.targetDirectory = targetDirectory; |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
@Override |
| 71 | |
public boolean accept(final File file) { |
| 72 | 0 | if (file.isHidden()) { |
| 73 | 0 | return false; |
| 74 | |
} |
| 75 | 0 | final String name = file.getName(); |
| 76 | 0 | if (file.isDirectory()) { |
| 77 | 0 | if (name.equalsIgnoreCase("target")) { |
| 78 | 0 | return false; |
| 79 | |
} |
| 80 | |
} |
| 81 | 0 | if (file.isFile()) { |
| 82 | 0 | if (name.equalsIgnoreCase("derby.log")) { |
| 83 | 0 | return false; |
| 84 | |
} |
| 85 | |
} |
| 86 | 0 | return !name.startsWith("."); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
@Override |
| 93 | |
public int compare(final File file1, final File file2) { |
| 94 | 0 | if (file1.isDirectory()) { |
| 95 | 0 | if (!file2.isDirectory()) { |
| 96 | 0 | return +1; |
| 97 | |
} |
| 98 | 0 | } else if (file2.isDirectory()) { |
| 99 | 0 | return -1; |
| 100 | |
} |
| 101 | 0 | return file1.getName().compareToIgnoreCase(file2.getName()); |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public void pack(final File sourceDirectory, final String targetFilename) throws IOException { |
| 112 | 0 | sourcePath = sourceDirectory.getPath(); |
| 113 | 0 | if (!sourcePath.endsWith(File.separator)) { |
| 114 | 0 | sourcePath += File.separatorChar; |
| 115 | |
} |
| 116 | 0 | final File outFile = new File(targetDirectory, targetFilename); |
| 117 | 0 | out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile))); |
| 118 | 0 | out.setLevel(9); |
| 119 | 0 | addEntries(sourceDirectory); |
| 120 | 0 | out.close(); |
| 121 | 0 | out = null; |
| 122 | 0 | sourcePath = null; |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
private void addEntries(final File directory) throws IOException { |
| 129 | 0 | final File[] content = directory.listFiles(this); |
| 130 | 0 | if (content == null) { |
| 131 | 0 | return; |
| 132 | |
} |
| 133 | 0 | Arrays.sort(content, this); |
| 134 | 0 | for (final File file : content) { |
| 135 | 0 | final boolean isDirectory = file.isDirectory(); |
| 136 | 0 | String name = file.getPath(); |
| 137 | 0 | if (!name.startsWith(sourcePath)) { |
| 138 | |
|
| 139 | 0 | throw new IOException("Illegal path: " + name); |
| 140 | |
} |
| 141 | 0 | name = name.substring(sourcePath.length()).replace(File.separatorChar, '/'); |
| 142 | 0 | if (isDirectory) { |
| 143 | 0 | name += '/'; |
| 144 | |
} |
| 145 | 0 | final ZipEntry entry = new ZipEntry(name); |
| 146 | 0 | entry.setTime(file.lastModified()); |
| 147 | 0 | if (isDirectory) { |
| 148 | 0 | entry.setMethod(ZipEntry.STORED); |
| 149 | 0 | entry.setSize(0); |
| 150 | 0 | entry.setCompressedSize(0); |
| 151 | 0 | entry.setCrc(0); |
| 152 | |
} else { |
| 153 | 0 | entry.setMethod(ZipEntry.DEFLATED); |
| 154 | 0 | entry.setSize(file.length()); |
| 155 | |
} |
| 156 | 0 | out.putNextEntry(entry); |
| 157 | 0 | if (!isDirectory) { |
| 158 | 0 | final InputStream in = new FileInputStream(file); |
| 159 | 0 | int n; while ((n = in.read(buffer)) >= 0) { |
| 160 | 0 | out.write(buffer, 0, n); |
| 161 | |
} |
| 162 | 0 | in.close(); |
| 163 | |
} |
| 164 | 0 | out.closeEntry(); |
| 165 | 0 | if (isDirectory) { |
| 166 | 0 | addEntries(file); |
| 167 | |
} |
| 168 | |
} |
| 169 | 0 | } |
| 170 | |
} |