final ZipOutputStream stream = new ZipOutputStream(fOut);
if (monitor.isCanceled()) {
// Nothing written, just close and cancel
stream.close();
throw new OperationCanceledException();
}
// Add each file to the zip stream
for (int i = 0; i < fExports.length; ++i) {
final IFile file = fExports[i];
// Verify file is child of root
if (!fRoot.getFullPath().isPrefixOf(file.getFullPath())) {
throw new IllegalArgumentException(fRoot + " is not a parent of " + file); //$NON-NLS-1$
}
// Read file contents
String filename = file.getFullPath().toPortableString();
final String cutPath = fRoot.getFullPath().addTrailingSeparator().toPortableString();
filename = filename.substring(cutPath.length());
monitor.subTask("Adding " + filename);
final byte[] fileContents = readContents(file, new SubProgressMonitor(monitor, SCALE));
// Add contents to zip file
final ZipEntry entry = new ZipEntry(filename);
// file.getModificationStamp() seems to use a different semantic
// for the timestamp
entry.setTime(file.getLocation().toFile().lastModified());
if (fCompress) {
entry.setMethod(ZipEntry.DEFLATED);
} else {
entry.setMethod(ZipEntry.STORED);
entry.setSize(fileContents.length);
final CRC32 checksumCalculator = new CRC32();
checksumCalculator.update(fileContents);
entry.setCrc(checksumCalculator.getValue());
}
stream.putNextEntry(entry);
stream.write(fileContents);
monitor.worked(SCALE);
if (monitor.isCanceled()) {
// Close zip file and remove it, then terminate
stream.close();
pearFile.delete();
throw new OperationCanceledException();
}
}
stream.close();
} catch (final FileNotFoundException exception) {