1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public byte[] zip(final List<DataFile> files) throws IOException {
if (files.isEmpty()) {
return new byte[]{};
}
logFiles(files, "zipping files :");
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ZipOutputStream zipfile = new ZipOutputStream(bos);
for (DataFile file : files) {
zipfile.putNextEntry(new ZipEntry(file.getName()));
zipfile.write(file.getData());
}
zipfile.close();
return bos.toByteArray();
}
public List<DataFile> unzip(final byte[] zipcontent, final int offset, final int length)
throws IOException {
final List<DataFile> files = new ArrayList<DataFile>();
final ByteArrayInputStream bis = new ByteArrayInputStream(zipcontent, offset, length);
ZipInputStream zipfile = null;
try {
zipfile = new ZipInputStream(bis);
ZipEntry entry;
while ((entry = zipfile.getNextEntry()) != null) {
final DataFile f = new SimpleFile(entry.getName(), StreamUtils.readInputStream(zipfile));
files.add(f);
}
} finally {
if (zipfile != null) {
zipfile.close();
}
}
return files;
}
private void logFiles(List<DataFile> files, String description) {
final StringBuffer buffer = new StringBuffer(description);
for (final DataFile file : files) {
buffer.append("\n");
buffer.append(file.getName());
}
logger.debug(buffer.toString());
} |
Partager