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 45 46 47 48 49 50 51 52
|
// These are the files to include in the ZIP file
List<String> filenames = down.getFilenames();
List<String> urls = down.getUrls();
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
HttpClient client = new HttpClient();
byte[] outputByteArray;
// set default retry handler
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// Create the ZIP file
String outFilename = "outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
// Compress the files
for (int i=0; i<filenames.size(); i++) {
//make an HTTP GET
HttpMethod method = new GetMethod(urls.get(i));
client.executeMethod(method);
//get bytes
outputByteArray = method.getResponseBody();
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames.get(i)));
// Transfer bytes from the file to the ZIP file
out.write(outputByteArray);
// Complete the entry
out.closeEntry();
}
// Complete the ZIP file
out.close();
//Download hte ZIP file
response.setContentType( "application/octet-stream" );
response.setHeader( "Content-Disposition", "attachment; filename=\""+outFilename+"\"" );
ServletOutputStream op = response.getOutputStream();
// ??????????? |
Partager