├── apex-zip
├── src
│ ├── staticresources
│ │ ├── jszip.resource
│ │ └── jszip.resource-meta.xml
│ ├── pages
│ │ ├── zipdemo.page-meta.xml
│ │ ├── unzipdemo.page-meta.xml
│ │ ├── importstaticresource.page-meta.xml
│ │ ├── unzipdemo.page
│ │ ├── zipdemo.page
│ │ └── importstaticresource.page
│ ├── classes
│ │ ├── MetadataService.cls-meta.xml
│ │ ├── UnzipDemoController.cls-meta.xml
│ │ ├── ZipDemoController.cls-meta.xml
│ │ ├── ImportStaticResourceController.cls-meta.xml
│ │ ├── UnzipDemoController.cls
│ │ ├── ZipDemoController.cls
│ │ └── ImportStaticResourceController.cls
│ ├── tabs
│ │ ├── ZipFile__c.tab
│ │ ├── UnzipDemo.tab
│ │ └── ImportStaticResource_Demo.tab
│ ├── applications
│ │ └── Zippy.app
│ ├── components
│ │ ├── zip.component-meta.xml
│ │ ├── unzip.component-meta.xml
│ │ ├── zipentry.component-meta.xml
│ │ ├── zipfile.component-meta.xml
│ │ ├── unzipfile.component-meta.xml
│ │ ├── zipentry.component
│ │ ├── zip.component
│ │ ├── unzipfile.component
│ │ ├── unzip.component
│ │ └── zipfile.component
│ ├── package.xml
│ ├── objects
│ │ └── ZipFile__c.object
│ └── layouts
│ │ └── ZipFile__c-Zip File Layout.layout
└── .project
├── LICENSE
└── README.markdown
/apex-zip/src/staticresources/jszip.resource:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/certinia/apex-zip/HEAD/apex-zip/src/staticresources/jszip.resource
--------------------------------------------------------------------------------
/apex-zip/src/pages/zipdemo.page-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 |
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/pages/unzipdemo.page-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 |
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/MetadataService.cls-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 26.0
4 | Active
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/UnzipDemoController.cls-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | Active
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/ZipDemoController.cls-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | Active
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/ImportStaticResourceController.cls-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | Active
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/pages/importstaticresource.page-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 |
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/tabs/ZipFile__c.tab:
--------------------------------------------------------------------------------
1 |
2 |
3 | true
4 | false
5 | Custom67: Gears
6 |
7 |
--------------------------------------------------------------------------------
/apex-zip/src/staticresources/jszip.resource-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Private
4 | application/zip
5 |
6 |
--------------------------------------------------------------------------------
/apex-zip/src/tabs/UnzipDemo.tab:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 | Custom67: Gears
6 | unzipdemo
7 |
8 |
--------------------------------------------------------------------------------
/apex-zip/src/tabs/ImportStaticResource_Demo.tab:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 | Custom67: Gears
6 | importstaticresource
7 |
8 |
--------------------------------------------------------------------------------
/apex-zip/src/applications/Zippy.app:
--------------------------------------------------------------------------------
1 |
2 |
3 | standard-home
4 |
5 | ImportStaticResource_Demo
6 | UnzipDemo
7 | ZipFile__c
8 |
9 |
--------------------------------------------------------------------------------
/apex-zip/src/components/zip.component-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | This is a generic template for Visualforce Component. With this template, you may adjust the default elements and values and add new elements and values.
5 |
6 |
7 |
--------------------------------------------------------------------------------
/apex-zip/src/components/unzip.component-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | This is a generic template for Visualforce Component. With this template, you may adjust the default elements and values and add new elements and values.
5 |
6 |
7 |
--------------------------------------------------------------------------------
/apex-zip/src/components/zipentry.component-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | This is a generic template for Visualforce Component. With this template, you may adjust the default elements and values and add new elements and values.
5 |
6 |
7 |
--------------------------------------------------------------------------------
/apex-zip/src/components/zipfile.component-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | This is a generic template for Visualforce Component. With this template, you may adjust the default elements and values and add new elements and values.
5 |
6 |
7 |
--------------------------------------------------------------------------------
/apex-zip/src/components/unzipfile.component-meta.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 24.0
4 | This is a generic template for Visualforce Component. With this template, you may adjust the default elements and values and add new elements and values.
5 |
6 |
7 |
--------------------------------------------------------------------------------
/apex-zip/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | apexzip
4 |
5 |
6 |
7 |
8 |
9 | com.salesforce.ide.builder.default
10 |
11 |
12 |
13 |
14 | com.salesforce.ide.builder.online
15 |
16 |
17 |
18 |
19 |
20 | com.salesforce.ide.nature.default
21 | com.salesforce.ide.nature.online
22 |
23 |
24 |
--------------------------------------------------------------------------------
/apex-zip/src/package.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ImportStaticResourceController
5 | MetadataService
6 | UnzipDemoController
7 | ZipDemoController
8 | ApexClass
9 |
10 |
11 | unzip
12 | unzipfile
13 | zip
14 | zipentry
15 | zipfile
16 | ApexComponent
17 |
18 |
19 | importstaticresource
20 | unzipdemo
21 | zipdemo
22 | ApexPage
23 |
24 |
25 | *
26 | CustomApplication
27 |
28 |
29 | *
30 | CustomObject
31 |
32 |
33 | *
34 | CustomTab
35 |
36 |
37 | ZipFile__c-Zip File Layout
38 | Layout
39 |
40 |
41 | jszip
42 | StaticResource
43 |
44 | 24.0
45 |
46 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c), FinancialForce.com, inc
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without modification,
5 | are permitted provided that the following conditions are met:
6 |
7 | - Redistributions of source code must retain the above copyright notice,
8 | this list of conditions and the following disclaimer.
9 | - Redistributions in binary form must reproduce the above copyright notice,
10 | this list of conditions and the following disclaimer in the documentation
11 | and/or other materials provided with the distribution.
12 | - Neither the name of the FinancialForce.com, inc nor the names of its contributors
13 | may be used to endorse or promote products derived from this software without
14 | specific prior written permission.
15 |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 | THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 |
--------------------------------------------------------------------------------
/README.markdown:
--------------------------------------------------------------------------------
1 | Apex Zip Support
2 | ================
3 |
4 |
5 |
7 |
8 |
9 | Background
10 | -----------
11 |
12 | I recently found a number of developers asking questions about Zip file handling in Apex, which as most find out pretty soon, does not exist. Statement governor concerns aside, nor is there any binary data types to make implementing support for it possible. Most understandably want to stay on platform and not call out to some external service for this. And rightly so!
13 |
14 | Strategies
15 | ----------
16 |
17 | I have describe the strategies, components and examples demonstrated in this repo in a series of blogs, the first of which is [here](http://andrewfawcett.wordpress.com/2012/11/04/handling-office-files-and-zip-files-in-apex-part-1/). The second and final part of the series can be found [here](http://andrewfawcett.wordpress.com/2012/12/09/handling-office-files-and-zip-files-in-apex-part-2/). Enjoy!
18 |
19 |
20 | About the Author
21 | ----------------
22 |
23 | My name is Andrew Fawcett, I am the CTO of FinancialForce.com, if you want to ask questions you can do so via the Issues tab or just follow me on Twitter, my name is [andyinthecloud](http://twitter.com/andyinthecloud)
24 |
25 | I enjoy making life easier and enabling more people to help me in this endevour! And thus API's is one of my main passions. Hence this article! Enjoy and do let me know what cool time saving solutions you create!
26 |
--------------------------------------------------------------------------------
/apex-zip/src/components/zipentry.component:
--------------------------------------------------------------------------------
1 |
28 |
29 |
30 |
31 |
32 |
35 |
--------------------------------------------------------------------------------
/apex-zip/src/objects/ZipFile__c.object:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Accept
5 | Default
6 |
7 |
8 | Clone
9 | Default
10 |
11 |
12 | Delete
13 | Default
14 |
15 |
16 | Edit
17 | Default
18 |
19 |
20 | List
21 | Default
22 |
23 |
24 | New
25 | Default
26 |
27 |
28 | Tab
29 | Default
30 |
31 |
32 | View
33 | Default
34 |
35 | Deployed
36 | false
37 | false
38 | false
39 | false
40 |
41 |
42 | All
43 | Everything
44 |
45 |
46 |
47 |
48 | Text
49 |
50 | Zip Files
51 |
52 | ReadWrite
53 |
54 | Zip
55 | online
56 | button
57 | 600
58 | page
59 | Zip
60 | sidebar
61 | zipdemo
62 | false
63 |
64 |
65 |
--------------------------------------------------------------------------------
/apex-zip/src/pages/unzipdemo.page:
--------------------------------------------------------------------------------
1 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/apex-zip/src/pages/zipdemo.page:
--------------------------------------------------------------------------------
1 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
37 |
38 |
39 |
40 |
41 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/apex-zip/src/layouts/ZipFile__c-Zip File Layout.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 | Zip
4 |
5 | false
6 | false
7 | true
8 |
9 |
10 |
11 | Required
12 | Name
13 |
14 |
15 |
16 |
17 | Edit
18 | OwnerId
19 |
20 |
21 |
22 |
23 |
24 | false
25 | false
26 | true
27 |
28 |
29 |
30 | Readonly
31 | CreatedById
32 |
33 |
34 |
35 |
36 | Readonly
37 | LastModifiedById
38 |
39 |
40 |
41 |
42 |
43 | true
44 | false
45 | true
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | RelatedNoteList
54 |
55 | false
56 | true
57 | true
58 | false
59 | false
60 |
61 | 00hG000000R1OPo
62 | 4
63 | 0
64 | Default
65 |
66 |
67 |
--------------------------------------------------------------------------------
/apex-zip/src/components/zip.component:
--------------------------------------------------------------------------------
1 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
40 |
41 |
50 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/UnzipDemoController.cls:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2012, FinancialForce.com, inc
3 | * All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without modification,
6 | * are permitted provided that the following conditions are met:
7 | *
8 | * - Redistributions of source code must retain the above copyright notice,
9 | * this list of conditions and the following disclaimer.
10 | * - Redistributions in binary form must reproduce the above copyright notice,
11 | * this list of conditions and the following disclaimer in the documentation
12 | * and/or other materials provided with the distribution.
13 | * - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14 | * may be used to endorse or promote products derived from this software without
15 | * specific prior written permission.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 | **/
26 |
27 | /**
28 | * Demonstrates how to use the component
29 | **/
30 | public with sharing class UnzipDemoController
31 | {
32 | // Unzipped component state, in this case the Id of the ZipFile__c record
33 | public String unzippedState {get;set;}
34 |
35 | public UnzipDemoController()
36 | {
37 | ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,
38 | 'Unzip processing will start once the file is selected, please wait to be redirected to the results after this.'));
39 | }
40 |
41 | // Action Function method called by client code once all entries have been unzipped
42 | public PageReference unzipped()
43 | {
44 | // Redirect to ZipFile record
45 | return new PageReference('/' + unzippedState);
46 | }
47 |
48 | @RemoteAction
49 | // JavaScript Remoting method called as each item is unzipped from the file
50 | public static String receiveZipFileEntry(String filename, String path, String data, String state)
51 | {
52 | // First file?
53 | if(state=='')
54 | {
55 | // Create a record to store unzipped files as attachments
56 | ZipFile__c zipFile = new ZipFile__c();
57 | zipFile.Name = filename;
58 | insert zipFile;
59 | state = zipFile.Id;
60 | }
61 |
62 | // Attach unzipped file
63 | Attachment attachment = new Attachment();
64 | attachment.Body = EncodingUtil.base64Decode(data);
65 | attachment.Name = path;
66 | attachment.ParentId = state;
67 | insert attachment;
68 |
69 | return state;
70 | }
71 | }
--------------------------------------------------------------------------------
/apex-zip/src/pages/importstaticresource.page:
--------------------------------------------------------------------------------
1 |
28 |
29 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
{!UnzippedFile}
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/ZipDemoController.cls:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2012, FinancialForce.com, inc
3 | * All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without modification,
6 | * are permitted provided that the following conditions are met:
7 | *
8 | * - Redistributions of source code must retain the above copyright notice,
9 | * this list of conditions and the following disclaimer.
10 | * - Redistributions in binary form must reproduce the above copyright notice,
11 | * this list of conditions and the following disclaimer in the documentation
12 | * and/or other materials provided with the distribution.
13 | * - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14 | * may be used to endorse or promote products derived from this software without
15 | * specific prior written permission.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 | **/
26 |
27 | /**
28 | * Demonstrates how to use the component
29 | **/
30 | public with sharing class ZipDemoController
31 | {
32 | // Zip file paths
33 | public List paths {get;set;}
34 |
35 | // Generated zip file data (see receiveZipFile)
36 | public transient String zipFileData {get;set;}
37 |
38 | private ApexPages.StandardController controller;
39 |
40 | public ZipDemoController(ApexPages.StandardController controller)
41 | {
42 | this.controller = controller;
43 |
44 | // Info message to ask the user to wait (no real time progress messages yet)
45 | ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,
46 | 'Zip processing for the attacments has started please wait, you will be redirected to the resulting Document once this is completed.'));
47 |
48 | // Use Attachment names as zip entry path names for this example
49 | List attachments = [select Name from Attachment where ParentId =: controller.getId()];
50 | paths = new List();
51 | for(Attachment attachment : attachments)
52 | paths.add(attachment.Name);
53 | }
54 |
55 | public PageReference receiveZipFile()
56 | {
57 | // Receive the generated zip file and store it (in this case as a Document)
58 | ZipFile__c zipFile = [select Name from ZipFile__c where Id =: controller.getId()];
59 | Document document = new Document();
60 | document.Name = zipFile.Name;
61 | document.FolderId = UserInfo.getUserId();
62 | document.Body = EncodingUtil.base64Decode(zipFileData);
63 | insert document;
64 | return new PageReference('/'+document.Id);
65 | }
66 |
67 | @RemoteAction
68 | public static String getZipFileEntry(String path, String state)
69 | {
70 | // Retrieve or generate the Base64 encoded data for each file path to be added to the zip
71 | Attachment attachment = [select Name, Body from Attachment where ParentId =:state and Name =:path];
72 | return EncodingUtil.base64Encode(attachment.body);
73 | }
74 | }
--------------------------------------------------------------------------------
/apex-zip/src/components/unzipfile.component:
--------------------------------------------------------------------------------
1 |
28 |
29 |
30 |
31 |
32 |
33 |
54 |
55 |
56 |
72 |
--------------------------------------------------------------------------------
/apex-zip/src/components/unzip.component:
--------------------------------------------------------------------------------
1 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
86 |
--------------------------------------------------------------------------------
/apex-zip/src/components/zipfile.component:
--------------------------------------------------------------------------------
1 |
28 |
29 |
31 |
33 |
35 |
37 |
38 |
39 |
40 |
41 |
106 |
--------------------------------------------------------------------------------
/apex-zip/src/classes/ImportStaticResourceController.cls:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2012, FinancialForce.com, inc
3 | * All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without modification,
6 | * are permitted provided that the following conditions are met:
7 | *
8 | * - Redistributions of source code must retain the above copyright notice,
9 | * this list of conditions and the following disclaimer.
10 | * - Redistributions in binary form must reproduce the above copyright notice,
11 | * this list of conditions and the following disclaimer in the documentation
12 | * and/or other materials provided with the distribution.
13 | * - Neither the name of the FinancialForce.com, inc nor the names of its contributors
14 | * may be used to endorse or promote products derived from this software without
15 | * specific prior written permission.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 | **/
26 |
27 | public with sharing class ImportStaticResourceController
28 | {
29 | public transient Blob Content {get; set;}
30 | public String UnzippedFile {get; set;}
31 | public MetadataService.AsyncResult AsyncResult {get; private set;}
32 | private static String RESOURCE_NAME = 'upload'+UserInfo.getUserId();
33 |
34 | public PageReference upload()
35 | {
36 | ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Uploading...'));
37 |
38 | // Metadata Service
39 | MetadataService.MetadataPort service = createService();
40 |
41 | // Map existing Static Resource files
42 | List queries = new List();
43 | MetadataService.ListMetadataQuery queryStaticResource = new MetadataService.ListMetadataQuery();
44 | queryStaticResource.type_x = 'StaticResource';
45 | queries.add(queryStaticResource);
46 | MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 26);
47 | Map mapFileProperties = new Map();
48 | for(MetadataService.FileProperties fileProperty : fileProperties)
49 | mapFileProperties.put(fileProperty.fullName, fileProperty);
50 |
51 | // Uploads the file as a Static Resource
52 | MetadataService.StaticResource staticResource = new MetadataService.StaticResource();
53 | staticResource.fullName = RESOURCE_NAME;
54 | staticResource.contentType = 'application/zip';
55 | staticResource.cacheControl = 'private';
56 | staticResource.description = 'Temporary upload to unzip file for user ' + UserInfo.getName();
57 | staticResource.content = EncodingUtil.base64Encode(Content);
58 | if(mapFileProperties.containsKey(RESOURCE_NAME))
59 | {
60 | // Update existing Static Resource
61 | List updateMetadatas = new List();
62 | updateMetadatas.add(new MetadataService.UpdateMetadata());
63 | updateMetadatas[0].currentName = RESOURCE_NAME;
64 | updateMetadatas[0].metadata = staticResource;
65 | AsyncResult = service.updateMetadata(updateMetadatas)[0];
66 | }
67 | else
68 | {
69 | // Create new Static Resource
70 | AsyncResult = service.create(new List { staticResource })[0];
71 | }
72 |
73 | return null;
74 | }
75 |
76 | public PageReference checkAsyncRequest()
77 | {
78 | // Check the status of the create request
79 | MetadataService.MetadataPort service = createService();
80 | MetadataService.AsyncResult[] aysncResults = service.checkStatus(new String[] { AsyncResult.Id });
81 | if(aysncResults[0].done)
82 | {
83 | // Errors?
84 | if(aysncResults[0].state == 'Error')
85 | {
86 | ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, aysncResults[0].message));
87 | AsyncResult = null;
88 | }
89 | else
90 | {
91 | // Upload complete reset sync
92 | ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Upload complete'));
93 | AsyncResult = null;
94 |
95 | // Unzip the /word/document.xml file
96 | PageReference contentTypes = new PageReference('/resource/' + RESOURCE_NAME + '/word/document.xml');
97 | UnzippedFile = contentTypes.getContent().toString();
98 | }
99 | }
100 | else
101 | {
102 | ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.INFO,'Uploading...'));
103 | }
104 | return null;
105 | }
106 |
107 | public static MetadataService.MetadataPort createService()
108 | {
109 | MetadataService.MetadataPort service = new MetadataService.MetadataPort();
110 | service.SessionHeader = new MetadataService.SessionHeader_element();
111 | service.SessionHeader.sessionId = UserInfo.getSessionId();
112 | return service;
113 | }
114 | }
--------------------------------------------------------------------------------