Wednesday, October 15, 2008

Zip and unzip files in a folder

Many a time we need to zip or unzip more then one file present in a folder. SSIS provides a easy way to handle this.

For my example I am using 7-zip though we have many other compress software like (pkzip or winzip)

Steps to zip/unzip files
A) Declare a variable FileName as string datatype

B) Use ForEach loop to loop through the files in the folder and map file names to FileName variable
a) Provide folder name inEnumerator configuration

b) map FileName Variable

C) Use Execute Process task to zip/unzip to individual files
a) Browse to Exe of 7z in Executable

b) Use expression to configure the argument

D) Execute the package

Use the files........


Anonymous said...

Dear Rahul,

Thank you for posting this topic, very very helpfull to see a step-by-step guide with screenshots.


Gahan Zwart

Alexis said...

Several days ago my friend told me about his unpleasant problem. He told me that he had damaged zip files. But he unexpectedly used - corrupt winzip file. And it helped him for seconds and free of charge as far as I remembered. Moreover it resolved my similar issue very easy.

Jeffery Williams said...

I am trying to do this in a stored procedure. The 7zip program is executing but the return is that it can not find the archive. I have a print statement to see what I am passing to the OS and if I copy / paste it my command line works. But does not find the file from SQL execution. Any ideas anyone.

C:\7zip\7zip\7z.exe e C:\Users\jwilliams\Documents\EConnect\FirstMerrit\SourceFiles\ pP810571

The above it the result of running my SQL script.

HEre is the actual SQL script:

ALTER PROCEDURE UnZipFile @CustomerID int


declare @ZipFileName VarChar(max)
declare @SQLCommand VarChar(max)
DECLARE @DataZipPass as varchar(10)

SELECT @DataZipPass = DataZipPass
FROM Customers
WHERE Customers.CustomerID = @CustomerID

SET @ZipFileName = 'C:\Users\jwilliams\Documents\EConnect\FirstMerrit\SourceFiles\'

SET @SQLCommand =
'exec master..xp_cmdshell ' + '''' + 'C:\7zip\7zip\7z.exe e '
+ @ZipFileName + ' p' +@DataZipPass + ' -y ' + ''''

PRINT 'Extraction Command sent: ' + cast(@SQLCommand as varchar(max))

EXEC (@SQLCommand)

Alex said...

Yesterday I couldn't open my zip file with my working information. I was upset, but to my good fortune my friend suggested me - problems with winzip file. He was right it settled my problem with ease and I at sight forgot about it.

ashish jindal said...
This comment has been removed by the author.
ashish jindal said...

Hello, it was a really good example. it helped me in completing Unzip file task. i have a small query...
i need the name of output file.
i provided param StandardOutputVariable = @[User::NewFileName]
but i can't capture the filename.
am i missing something

Rahul Kumar said...

@ashish: to get output file name depends on the utility you would use.
StardardOutputVariable will get all the information return from the process,if the utility returns the just the file are gud but usually Utilities return lot of information back like success status, size etc and file name is just one of them.

Anonymous said...

I getting the following error: The process exit code was "7" while the expected was "0"

Can you please help me how I can fix this error.


Rahul Kumar said...

@ "I getting the following error: The process exit code was "7" while the expected was "0"

Can you please help me how I can fix this error"

There must be some error, might be in command stmt.. could you get the stmt and run it in command prompt or paste here so that i can look at it.

shahbaz said...


Rahul Kumar said...


TP5 said...

Hi...I'm also getting the prcess exit code was 7 - please help

TP5 said...

I'm also getting the error:the process exit code was "7" while the expected was "0" - please help

David Roodman said...

Thanks, this was just what I needed. It solved my problem and gave me a crash course in programming integration services packages.

Mike Stuugie said...

Hello Rahul,

Can you please explain your arguments used?

user1 said...

Iam getting below error ,Please help
The process exit code was "7" while the expected was "0".

Anonymous said...

Hi, I tried to unzip a file using 7zip at command line with the following command:

C:\Program Files\7-Zip>7z e c:\ -oc:\new folder\

I got the following error:
No files to process

Files: 0
Size: 0
Compressed: 4790659

C:\Program Files\7-Zip>

Please help.

What am I doing wrong?

Niall said...

Hurray, thanks! Worked first time.

Anonymous said...

I appreciate your tutorial it helped me a lot! Thank You!!

Anonymous said...

what is @[User::NewFileName]

Domain KING said...

Thank you for such a wonderful Information !!

Veritas Cluster Interview Questions

SAMBA Server Interview Questions

Linux FTP vsftpd Interview Questions

SSH Interview Questions

Apache Interview Questions

Nagios Interview questions

IPTABLES Interview Questions

Ldap Server Interview Questions

LVM Interview questions

Sendmail Server Interview Questions

YUM Interview Questions

NFS Interview Questions

Read More at :- Linux Troubleshooting

lee woo said...

Age is an issue of mind over matter. If you don't mind, it doesn't matter. See the link below for more info.


Unknown said...

Hi Rahul,

Could you please tell me What 'e -oc:/rahul' is in Argument??

Basudev Yadav said...

You have made nice post but you can know more about 7-zip (compressor/extractor) it at:

Post a Comment