if (req.getParameter(ProtocolConstants.PARAM_EXCLUDE) != null) {
excludedFiles = Arrays.asList(req.getParameter(ProtocolConstants.PARAM_EXCLUDE).split(",")); //$NON-NLS-1$
}
/* exclude .git if already present in the destination root, see bug 428657 */
IFileStore destinationRoot = NewFileServlet.getFileStore(req, destPath);
IFileStore dotGitStorage = destinationRoot.getChild(".git"); //$NON-NLS-1$
if (dotGitStorage.fetchInfo().exists())
excludedFiles.add(".git"); //$NON-NLS-1$
try {
ZipFile source = new ZipFile(new File(getStorageDirectory(), FILE_DATA));
Enumeration<? extends ZipEntry> entries = source.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
IFileStore destination = destinationRoot.getChild(entry.getName());
if (!destinationRoot.isParentOf(destination) || hasExcludedParent(destination, destinationRoot, excludedFiles)) {
//file should not be imported
continue;
}
if (entry.isDirectory())
destination.mkdir(EFS.NONE, null);
else {
if (!force && destination.fetchInfo().exists()) {
filesFailed.add(entry.getName());
continue;
}
destination.getParent().mkdir(EFS.NONE, null);
// this filter will throw an IOException if a zip entry is larger than 100MB
FilterInputStream maxBytesReadInputStream = new FilterInputStream(source.getInputStream(entry)) {
private static final int maxBytes = 0x6400000; // 100MB
private int totalBytes;
private void addByteCount(int count) throws IOException {
totalBytes += count;
if (totalBytes > maxBytes) {
throw new IOException("Zip file entry too large");
}
}
@Override
public int read() throws IOException {
int c = super.read();
if (c != -1) {
addByteCount(1);
}
return c;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = super.read(b, off, len);
if (read != -1) {
addByteCount(read);
}
return read;
}
};
boolean fileWritten = false;
try {
IOUtilities.pipe(maxBytesReadInputStream, destination.openOutputStream(EFS.NONE, null), false, true);
fileWritten = true;
} finally {
if (!fileWritten) {
try {
destination.delete(EFS.NONE, null);
} catch (CoreException ce) {
// best effort
}
}
}