Copyright (c) 2015 Pedro Dal Col, Pliny Smith
Native Apex Zip utility for the salesforce.com platform.
Copy and paste from this repository the following four classes into new classes in your Salesforce instance.
- HexUtil.cls
- Puff.cls
- Zippex.cls
- ZippexTests.cls
public Zippex()
Instantiates a new empty Zippex object (empty Zip archive).
Zippex sampleZip = new Zippex();
public Zippex(Blob fileData)
Instantiates a new Zippex object from an existing Zip file.
Name | Type | Description |
---|---|---|
fileData | Blob | Data containing a valid Zip file |
Attachment sampleAttachment = [SELECT Name, Body FROM Attachment WHERE Id='<ID_OF_ATTACHMENT>'];
Zippex sampleZip = new Zippex(sampleAttachment.Body);
public void addFile(String fileName, Blob fileData, String crc32)
Adds a new file to the current Zip archive.
Name | Type | Description |
---|---|---|
fileName | String | File name including full path |
fileData | Blob | Data containing the file data |
crc32 | String | (optional) little endian hex value of the CRC32. Enter null for addFile to calculate the CRC32 of the fileData |
Zippex sampleZip = new Zippex();
Blob fileData = Blob.valueOf('Sample text.');
sampleZip.addFile('sampleFolder/test.txt', fileData, null);
Blob zipData = sampleZip.getZipArchive();
public Boolean containsFile(String fileName)
Returns true if the current Zip archive contains the specified file.
Name | Type | Description |
---|---|---|
fileName | String | File name including full path |
Return | Boolean | Return true if file is in Zip archive |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/test.txt', Blob.valueOf('Sample text.'), null);
System.assert(sampleZip.containsFile('sampleFolder/test.txt'));
public Set<String> getFileNames()
Returns a set of filenames from the current Zip archive.
Name | Type | Description |
---|---|---|
Return | Set<String> | Returns all file names including full path in the current Zip archive |
Attachment sampleAttachment = [SELECT Name, Body FROM Attachment WHERE Id='<ID_OF_ATTACHMENT>'];
Zippex sampleZip = new Zippex(sampleAttachment.Body);
Set <String> fileNames = sampleZip.getFileNames();
for (String fileName : fileNames)
{
System.debug('file: ' + fileName);
}
public Blob getFile(String fileName)
Extracts the specified file contents from the current Zip archive. If the file does not exist, returns null.
Name | Type | Description |
---|---|---|
fileName | String | File name including full path |
Return | Blob | File data |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/test.txt', Blob.valueOf('Sample text.'), null);
Blob fileData = sampleZip.getFile('sampleFolder/test.txt');
System.assertEquals ('Sample text.', fileData.toString());
public Map<String,String> getFileInfo(String fileName)
Returns file metadata (lastModDateTime, crc32, fileSize, fileName, and fileComment).
Name | Type | Description |
---|---|---|
fileName | String | File name including full path |
Return | Map<String,String> | Contains values for lastModDateTime, crc32, fileSize, fileName, and fileComment |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/test.txt', Blob.valueOf('Sample text.'), null);
Map <String, String> fileInfoMap = sampleZip.getFileInfo('sampleFolder/test.txt');
System.assertEquals ('12', fileInfoMap.get('fileSize'));
System.assertEquals ('sampleFolder/test.txt', fileInfoMap.get('fileName'));
public Blob getZipArchive()
Returns a Blob that contains the entire Zip archive.
Name | Type | Description |
---|---|---|
Return | Blob | Full Zip archive data |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/test.txt', Blob.valueOf('Sample text.'), null);
Blob zipData = sampleZip.getZipArchive();
public void removeFile(String fileName)
Removes a file from the current Zip archive.
Name | Type | Description |
---|---|---|
fileName | String | File name to remove from Zip archive including full path |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/file1.txt', Blob.valueOf('Sample text1.'), null);
sampleZip.addFile('sampleFolder/file2.txt', Blob.valueOf('Sample text2.'), null);
System.assert(sampleZip.getFileNames().contains('sampleFolder/file1.txt'));
System.assert(sampleZip.getFileNames().contains('sampleFolder/file2.txt'));
sampleZip.removeFile('sampleFolder/file1.txt');
System.assert(sampleZip.getFileNames().contains('sampleFolder/file1.txt') == false);
System.assert(sampleZip.getFileNames().contains('sampleFolder/file2.txt'));
public void renameFile(String oldName, String newName)
Renames a file in the current Zip archive.
Name | Type | Description |
---|---|---|
oldName | String | The current file name to be modified |
newName | String | The new name that replaces the current file name |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/file.txt', Blob.valueOf('Sample text1.'), null);
System.assert(sampleZip.getFileNames().contains('sampleFolder/file.txt'));
sampleZip.renameFile('sampleFolder/file.txt', 'sampleFolder/changedName.txt');
System.assert(sampleZip.getFileNames().contains('sampleFolder/file.txt') == false);
System.assert(sampleZip.getFileNames().contains('sampleFolder/changedName.txt'));
public void removePrefix(String prefix)
Removes the specified prefix from all file names in the current Zip archive only if it occurs at the beginning of the file name.
Name | Type | Description |
---|---|---|
prefix | String | The prefix to remove from file names |
Zippex sampleZip = new Zippex();
sampleZip.addFile('sampleFolder/file.txt', Blob.valueOf('Sample text1.'), null);
System.assert(sampleZip.getFileNames().contains('sampleFolder/file.txt'));
sampleZip.removePrefix('sample');
System.assert(sampleZip.getFileNames().contains('sampleFolder/file.txt') == false);
System.assert(sampleZip.getFileNames().contains('Folder/file.txt'));
public static void unzipAttachment(Id srcAttId, Id destObjId, String[] fileNames, Boolean attemptAsync){
Name | Type | Description |
---|---|---|
srcAttId | Id | ID of the attachment to unzip |
destObjId | Id | ID of the object to which unzipped files should be attached. If null the ParentId of the Zip archive will be used |
fileNames | String[] | List containing file names to uncompress. If null, all files will be uncompressed |
attemptAsync | Boolean | If true, it attempts to unzip files in a future call |
Zippex.unzipAttachment('<ID_OF_ATTACHMENT>', null, null, false);