function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mariagusmariagus 

Unzip a file - any new?

Hi,

 

I saw several posts asking the same question. Is there a way to unzip a file in Apex in the latest release?

 

I could see that the best solution would be to use an external app, but I think this could be a risk for several reasons. I was also thinking about a JavaScript solution, but I'm not sure if this will work.

 

I would really appreciate any idea that I could follow to finish the process and make our customer happy.

 

Thanks

Navatar_DbSupNavatar_DbSup

Hi,

 

Well I did some searching before replying to your post, I didn't come across any function or class in Apex which does that, but perhaps you can unzip a file using JavaScript in case you want to include this functionality on a Visual Force page, one can call the Shell command equivalent in JavaScript and you can run the exe of WinZip and the file you want to unzip as parameter to the winzip.exe.

 

But then I realized JavaScript has no file access whatsoever unless you run it via a standalone program such as Adobe Integrated Runtime rather than using a web browser. AIR provides add-ons to JavaScript which provide it with additional features such as file access which are not available from web pages.

So you know you can do it the round way upload the zipped file as a Static resource and then access its components , the Apex way, if all you want to do is get the file contents in the zipped file.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

mariagusmariagus

Hi,

 

Thanks for your reply. It's very useful to know that JS would not work. It helps me to not to spend more time doing some researching about this.

 

Actually we don't mind using a VF page, or whaterver, and also the Static Resource was one of our first ideas, but I tried with no sucess.

 

What I did: Once I have the zip file as a new Static Resource, I tried to query it from the Controller, and access to its body, but I was not able to get the information fine, it was compressed, so not useful for us.

 

public String getStaticResourceFile()
{ 
    String sResourceName = 'MyZipFile';
    StaticResource myResource = [Select Id, Name, Body From StaticResource Where Name = :sResourceName];
    
    String myCSVFile = myResource.Body.toString();
    System.debug('myCSVFile = ' + myCSVFile);
    return myCSVFile;
}

 

Do you know any other way to do it?

 

Thanks for you help. Your post gave me some hope.

mariagusmariagus

Hi again,

 

I followed researching and I found this post but when I tried I got this error message: 404 status code return from request to http://ap1.salesforce.com/resource/1338463391000/myZipTest/myZipTest.csv?inline=1

 

Any idea?

 

This is my test code:

 

public String getCSVFile()
{
    String resourceName = 'myZipTest';
		
    String statResourceURL = getResourceURL(resourceName);
    PageReference pageRef = 
        new PageReference(statResourceURL + '/myZipTest.csv');
		
    // Get the content of this PageReference as a Blob
    Blob csvBlob = pageRef.getContent();
		
    String myfile = csvBlob.toString();
    return myfile;
}
	
public static String getResourceURL(String resourceName)
{
    List<StaticResource> resourceList = [SELECT 
                                                Name, 
                                                NamespacePrefix, 
                                                SystemModStamp
					 FROM StaticResource
					 WHERE Name = :resourceName];

    if (resourceList.size() == 1)
    {
        String namespace = resourceList[0].NamespacePrefix;
   	return '/resource/' + 
   		resourceList[0].SystemModStamp.getTime() + '/' + 
   		(namespace != null && namespace != '' ? namespace + '__': '') + 
  	        resourceName; 
    }
    else
        return '';
}

 

NageshTNageshT

HI,

 

I am Nagesh. I am new dev for Force.com.

I want to find, How can we read files using apex language on force.com platform.

 

Is there any way to read property files???

 

I have tried following ways to read file but i havn't get success.

1) create simple file 'Temp.properties' in staticResource in force IDE. and try to populate it from StaticResource Table.

2) Using file app upload a file and try to populate it from Document Table.

In both case, No rows generated.

 

Please help me to solve this problem.

Send mail on::

nagesh.takle@gmail.com

 

Thanks in advanced

--Nagesh

mariagusmariagus

Hi,

 

Would you mind creating a different post with your question? I will reply you there, and in case I'm not useful, some other people will be able to give you their feedback. If you leave your post here, no one ese will find it as the topic is different from mine.

 

Also, could you reply to this question? Do you need to read a file or do you need to create a file on a Document or Static Resource. Is this a csv file or has any fixed structure?

 

Thanks

NageshTNageshT

HI,

 

Thanks for giving reply to this message.

 

I have created separate another post for the same.

 

My requirement is to read property (Key=Value pair format ) file using Apex language on force.com.

 

I dont know how to store file on force.com and how to access it to read via coding.

Is apex provide any APIS to read file ???

Is there any class like java Properties class to read property files ???

 

E.g I have 'Temp.properties' file having Key=Value pair data. I have to read it and store this data locally and used it at runtime.

 

I hope u get my problem now. Please help me ASAP.

 

Thanks in Advanced

--Nagesh

Pedro I Dal ColPedro I Dal Col
The Zippex library alows you to unzip files in Apex. https://github.com/pdalcol/Zippex

For example to read the names of the files in a zip archive and their content you can use this code:
 
Attachment sampleAttachment = [SELECT Name, Body FROM Attachment WHERE Id='<ID_OF_ATTACHMENT>'];

Zippex sampleZip = new Zippex(sampleAttachment.Body);

//Loop through all files in zip
for (String fileName : sampleZip.getFileNames())
{
    System.debug('file name: ' + fileName);
    System.debug('file content: ' + sampleZip.getFile(fileName).toString());
}