if (!workingDir.exists() && !workingDir.mkdirs())
{
String message = "Unable to create the working directory ("
+ workingDir.getAbsolutePath() + ") to build the zip file.";
LOGGER.log(Level.FINE, message);
throw new InternalServerErrorException(message);
}
try
{
// Retrieve the reference file.
Reference reference = resource.getReference();
File refFile = new File(new URI(reference.getDataStoreReference()));
if (!refFile.exists())
{
throw new BadRequestException("Unable to locate the source file for the"
+ " reference.");
}
// Try to remove previously created zip files that have the same name.
String workingDirPath = workingDir.getCanonicalPath();
workingDirPath += workingDirPath.endsWith("/") ? "" : "/";
File file = new File(workingDirPath + refFile.getName() + ".zip");
if (file.exists() && !file.delete())
{
String message = "Unable to delete an existing zip file ("
+ file.getAbsolutePath()
+ ") before creating a new zip file with the same name.";
LOGGER.log(Level.FINE, message);
throw new InternalServerErrorException(message);
}
// Add the reference file to the zip file.
ZipFile zipFile = new ZipFile(file);
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.addFile(refFile, parameters);
httpHeaders.add("Content-Type", "application/zip");
httpHeaders.add("Content-Disposition",
"attachment; filename=\"" + file.getName() + "\"");
FileInputStream fis = new FileInputStream(file);
IOUtils.copy(fis, entityStream);
fis.close();
}
catch (URISyntaxException e)
{
String message =
"Problem with the data store URI for the reference source file(s).";
LOGGER.log(Level.FINE, message, e);
throw new NotFoundException(message + " " + e.getMessage());
}
catch (ZipException e)
{
String message =
"Unable to create a zip archive of the reference source file(s).";
LOGGER.log(Level.FINE, message, e);
throw new InternalServerErrorException(message + " " + e.getMessage());
}
}