Compress files to .zip format

Avni Nanda
July 14, 2016 12:39 ANSWERED
Hi ,

I am trying to zip files at a folder location .
I am unable to open application > "Windows explorer "
Also record sequence is not feasible as these files are created during the process workflow .

You help is much appreciated . Thankyou.

Regards

Avni

2 Likes

Andrzej.Kniola July 14, 2016 12:56
Hi Avni,

System.IO.Compression.ZipFile should help you - here is MSDN example that can be easily made with assigns and InvokeMethod activity.

Edit:

Also when working with paths be sure to check out Path.Combine, as it makes it a lot clearer and efficient to build.

Regards,

Andrzej

Eduardo Bernabe Sacahui Diaz September 09, 2016 21:09
Hello and thank you for your help, There is no .Zipfile target type in uipath community I guess :frowning: V2016.2

Cosmin Nicolae September 10, 2016 22:10
Hello.

You can use powershell to create zip archives. Here is a link to an example script. You need to use the Invoke Powershell Activity. If you at:

param($source,$destination)

at the start of the script you give values to those variables by defining them (with the same name) in the Parameters Proprieties of the Invoke Powershell Activity.

Regards,

Cosmin

Andrzej.Kniola September 12, 2016 16:15
Hello,

@Eduardo - it isn’t a UiPath issue. The classes needed are in System.IO.Compression.FileSystem.dll (not imported by default, not only in UiPath but VS also). You will need to add a package with reference to it, which might be overkill - Cosmin’s way is much simpler.

If you want to use the .Net classes, there is a trick by the way… Only do this if you know what you’re doing, you might break your file if you mess up the structure :slight_smile:

If you open the .xaml in any text editor (Notepad++ is great for that), find assembly reference lines and the one you need, it will load it there.

So for example find this line:

UiPath.Excel

And just after it add (note it’s case sensitive!)

System.IO.Compression.FileSystem

It will then load the assembly you need to use the ZipFile class (and ZipFile extensions are there too, they’re pretty useful).

Using it that way you may load any assembly that is located in you most-up-to-date Reference Assemblies folder (by default: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework[your_version])

Regards,

Andrzej

8 Likes

Eduardo Bernabe Sacahui Diaz September 14, 2016 15:56
@Cosmin Nicolae … Awesome, thank you i was using a power shell script but a iteration in UiPath to delete the zip files.

@ Andrzej.Kniola WOW! that´s amazing I want to learn more about modifying the xaml in Notepad++ I tried and it works like a charm :slight_smile:

Best regards Amigos !!!

Andrzej.Kniola September 14, 2016 17:11
For vast majority of the things you’d want to do using the Studio is quicker and safer. This is just one of the corner cases (~1% maybe) that actually editing the .xaml directly is useful for.

Please note that since the xaml is autogenerated by the Studio, if you modify it directly you might get strange result, corrupt the file or your changes will be reversed next time you modify the workflow in Studio.

Regards,

Andrzej

Hi

You can open particular zip application like 7Zip and after files had generated you can open that application and indicate with absolute path of that you wants to compress. and hit enter then click on ADD and hit enter.

Thanks

2 Likes

If all you need is the basic functionality that you explained, 7-zip seems like a good choice for you. It’s lightweight and fast with little bloat. Of course, it can do the basic things like create password protected archives and compress files. The 7z file format has a very good compression ratio.

Balmer

It is very helpful.
Is there any way I could add multiple (4-5) files in the parameter of invoke method and create a single zip file for it?

@igul Can you recreate this in Uipath.

 string[] files= Directory.GetFiles(filePath);
        // Create and open a new ZIP file
                    var zip = ZipFile.Open(fileName, ZipArchiveMode.Create);
                    foreach (var file in files)
                    {
                        // Add the entry for each file
                        zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
                    }
                    // Dispose of the object when we are done
                    zip.Dispose();
1 Like

Interesting…
'ZipArchive' does not have a public instance method named 'CreateEntryFromFile' matching the parameter types, generic type arguments, and generic type constraints supplied to InvokeMethod 'Invoke method'

Interestingly, in the same exact workflow I have a working custom activity that uses this exact call with those params. Checked without CompressionLevel as well (it has a second overload without it), no difference. Restarting etc. doesn’t solve it.

I mean, it’s not an issue for me, since the activity we’ve added is already done and it’s just a couple of lines anyway, but still odd.
Ideas?

1 Like

For some reason I don’t see ZipArchive (Compression.FileSystem) class, I’m using 2016.2 , Windows 2012 R2

You need to manually add the reference to System.IO.Compression.FileSystem in the xaml and restart (see post 5 here).
I’m wondering if it’s not loading it correctly though.

Sometimes I’d wish we could just add references to .dll’s without jumping through endless hoops :frowning:

2 Likes

Good tip. Works for me without Invokemethod as the Retrun type is “ZipArchiveEntry”, but unable to open the Zip file.Am i missing something?

Compress.xaml (9.2 KB)

3 Likes

Please ignore, i forgot to dispose the ZipArchive. Works now.

Compress.xaml (9.2 KB)

6 Likes

while running this code getting unauthorized access exception

Hi @sowmya,

That error is maybe because your zipPath is a folder.

You need to assign it the new zip file to be created(ex: “C:\Users\admin\Documents\test.zip”).
Then, assign to the filepath variable the folder path to get the files to be compressed(ex: “C:\Users\admin\Documents”).

It should work without errors after doing this.

1 Like

yes its working fine thank you

I want to attach the zip file and send smtp mail how to do this one?