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........

21 comments:

  1. Dear Rahul,

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

    Namasté,

    Gahan Zwart

    ReplyDelete
  2. 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\810571sn_4.zip pP810571

    The above it the result of running my SQL script.

    HEre is the actual SQL script:

    ALTER PROCEDURE UnZipFile @CustomerID int

    AS

    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\810571sn_4.zip'


    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))
    PRINT ''

    EXEC (@SQLCommand)

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. 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

    ReplyDelete
  5. @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 name..you are gud but usually Utilities return lot of information back like success status, size etc and file name is just one of them.

    ReplyDelete
  6. 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.


    Thanks

    ReplyDelete
  7. @ "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.

    ReplyDelete
  8. THANKS FOR EXCELLENT WORK RAHUL. I APPLIED TO MY TEST AND IT WORKS LIKE CHARM.

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

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

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

    ReplyDelete
  12. Hello Rahul,

    Can you please explain your arguments used?

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

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

    C:\Program Files\7-Zip>7z e c:\easy.zip -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?

    ReplyDelete
  15. Hurray, thanks! Worked first time.

    ReplyDelete
  16. I appreciate your tutorial it helped me a lot! Thank You!!
    Jeff

    ReplyDelete
  17. what is @[User::NewFileName]

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


    #issue
    www.ufgop.org

    ReplyDelete
  19. Hi Rahul,

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

    ReplyDelete
  20. You have made nice post but you can know more about 7-zip (compressor/extractor) it at:
    https://basusoftgame.blogspot.com/2017/12/7-Zip-Unzip-file-for-Windows.html

    ReplyDelete

Google Dataset Search

Google's Vision statement  is “ to provide access to the world's information in one click. ” Google’s mission Statement is “ ...