Skip to content

Instantly share code, notes, and snippets.

@magnet88jp
Last active October 14, 2016 01:15
Show Gist options
  • Save magnet88jp/c502ef48f68837c51b40c051c1b17712 to your computer and use it in GitHub Desktop.
Save magnet88jp/c502ef48f68837c51b40c051c1b17712 to your computer and use it in GitHub Desktop.
Apex Demo for zip file download
public with sharing class ZipDownloadDemoController {
// Constructor
public ZipDownloadDemoController() {
}
@RemoteAction
public static PageReference getZipDownloadUrl(String objectId) {
Zippex newZip = new Zippex();
List<Attachment> attachments = [SELECT Name, Body FROM Attachment WHERE ParentId = :objectId];
for(Attachment att : attachments ) {
newZip.addFile('folder/' + att.Name, att.Body, null);
}
Blob zipData = newZip.getZipArchive();
Document d = new Document();
d.name = 'demo.zip';
d.body = zipData;
d.folderId = UserInfo.getUserId();
insert d;
PageReference pageRef = new PageReference('/servlet/servlet.FileDownload?file=' + d.Id);
pageRef.setRedirect(true);
return pageRef;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment