Compress folder in to a zip file

  • hi ,

    I want to compress a folder into a zip file using ssis 2008

    please let me know how to do that , i tried few VB & C# script .

    but couldnt succeded . please help me .

    thanks 🙂

    take care

    Chhaya

  • What did you try? What were the errors?

    You could use the Execute Process Task to call the Winzip executable. Use the correct command line parameters and it should be no problem.

    Or just use .NET script task:

    http://microsoft-ssis.blogspot.com/2011/01/zip-sourcefile-to-archive.html

    (I found this article using Google with the keywords "ssis zip folder" :w00t:)

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • i try follwing c#script in script task

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.IO;

    using System.Windows.Forms;

    using java.util;

    using java.util.zip;

    using java.io;

    namespace BHI.Rats

    {

    /// <summary>

    /// compression component

    /// </summary>

    public static class RatsCompressionManager

    {

    #region Compress and Uncompress

    /// <summary>

    /// Zips the folder name to create a zip file.

    /// </summary>

    /// <remarks>The function is used for running as a worker thread</remarks>

    /// <param name="parameters">An array of 2 objects - folderName and zipFileName</param>

    public static void Zip(object parameters)

    {

    object[] parms = (object[])parameters;

    Zip((string)parms[0], (string)parms[1]);

    }

    /// <summary>

    /// Zips the folder name to create a zip file

    /// </summary>

    /// <param name="folderName"></param>

    /// <param name="zipFileName"></param>

    public static void Zip(string folderName, string zipFileName)

    {

    try

    {

    ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();

    fz.CreateZip(zipFileName, folderName, true, "");

    }

    catch (ICSharpCode.SharpZipLib.SharpZipBaseException)

    {

    //Fail silently on cancel

    if (Directory.Exists(folderName))

    {

    Directory.Delete(folderName, true);

    }

    if (System.IO.File.Exists(zipFileName))

    {

    System.IO.File.Delete(zipFileName);

    }

    }

    catch

    {

    //Close silently

    }

    }

    /// <summary>

    /// Compress list of files [sourceFiles] to [zipFileName]

    /// </summary>

    /// <param name="zipFileName"></param>

    /// <param name="sourceFiles"></param>

    public static void Zip(string zipFileName, FileInfo[] sourceFiles)

    {

    try

    {

    // get the root folder. NOTE: it assums that the first file is at the root.

    string rootFolder = Path.GetDirectoryName(sourceFiles[0].FullName);

    // compress

    ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();

    fz.CreateZip(zipFileName, rootFolder, true, "");

    }

    catch (Exception ex)

    {

    throw ex;

    }

    }

    /// <summary>

    /// Extract [zipFileName] to [destinationPath]

    /// </summary>

    /// <param name="zipFileName"></param>

    /// <param name="destinationPath"></param>

    public static void Extract(string zipFileName, string destinationPath)

    {

    try

    {

    ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();

    fz.ExtractZip(zipFileName, destinationPath, "");

    }

    catch (Exception ex)

    {

    throw ex;

    }

    }

    /// <summary>

    /// Extract [zipFileName] to [destinationPath]

    /// </summary>

    /// <param name="zipFileName"></param>

    /// <param name="destinationPath"></param>

    /// <param name="fileNameToExtract">need to be the relative path in the zip file</param>

    public static void ExtractSingleFile(string zipFileName, string destinationPath, string fileNameToExtract)

    {

    try

    {

    ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();

    fz.ExtractZip(zipFileName, destinationPath, fileNameToExtract);

    }

    catch (Exception ex)

    {

    throw ex;

    }

    }

    /// <summary>

    /// Return a list of zip entries (compressed files meta data)

    /// </summary>

    /// <param name="zipFile"></param>

    /// <returns></returns>

    private static List<ZipEntry> getZippedFiles(ZipFile zipFile)

    {

    List<ZipEntry> zipEntries = new List<ZipEntry>();

    java.util.Enumeration zipEnum = zipFile.entries();

    while (zipEnum.hasMoreElements())

    {

    ZipEntry zip = (ZipEntry)zipEnum.nextElement();

    zipEntries.Add(zip);

    }

    return zipEntries;

    }

    /// <summary>

    /// Get the list of files in the zip file

    /// </summary>

    /// <param name="zipFileName"></param>

    /// <returns></returns>

    public static System.Collections.ArrayList GetZipEntries(string zipFileName)

    {

    System.Collections.ArrayList arrZipEntries = new System.Collections.ArrayList();

    ZipFile zipfile = new ZipFile(zipFileName);

    List<ZipEntry> zipFiles = getZippedFiles(zipfile);

    foreach (ZipEntry zipFile in zipFiles)

    {

    arrZipEntries.Add(zipFile.getName());

    }

    zipfile.close();

    System.Collections.ArrayList arrZipEntriesInside = new System.Collections.ArrayList();

    foreach (string strZipFile in arrZipEntries)

    {

    if (string.Compare(Path.GetExtension(strZipFile), ".zip", true) == 0)

    {

    ExtractSingleFile(zipFileName, Path.GetDirectoryName(zipFileName), Path.GetFileName(strZipFile));

    ZipFile zipfileinside = new ZipFile(Path.GetDirectoryName(zipFileName) + Path.DirectorySeparatorChar + strZipFile);

    List<ZipEntry> zipFilesInside = getZippedFiles(zipfileinside);

    foreach (ZipEntry zipFile in zipFilesInside)

    {

    arrZipEntriesInside.Add(zipFile.getName());

    }

    zipfileinside.close();

    }

    }

    foreach (string strFilesInsideZip in arrZipEntriesInside)

    {

    arrZipEntries.Add(strFilesInsideZip);

    }

    return arrZipEntries;

    }

    /// <summary>

    /// Extract [zipFileName] to [destinationPath] recursively

    /// </summary>

    /// <param name="zipFileName"></param>

    public static void ExtractRecursively(string zipFileName)

    {

    try

    {

    ICSharpCode.SharpZipLib.Zip.FastZip fz = new ICSharpCode.SharpZipLib.Zip.FastZip();

    fz.ExtractZip(zipFileName, Path.GetDirectoryName(zipFileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(zipFileName), "");

    DirectoryInfo diInputDir = new DirectoryInfo(Path.GetDirectoryName(zipFileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(zipFileName));

    FileInfo[] fiInfoArr = diInputDir.GetFiles("*.zip", SearchOption.AllDirectories);

    foreach (FileInfo fiInfo in fiInfoArr)

    {

    if (string.Compare(Path.GetExtension(fiInfo.FullName), ".zip", true) == 0)

    {

    ExtractRecursively(fiInfo.FullName);

    }

    }

    }

    catch (Exception ex)

    {

    throw ex;

    }

    }

    #endregion

    }

    }

    but it is gving error while runing , that cant load the script . build is successfull .

  • thanks Koen Verbeeck,

    i tried script from given link , but its file by file zipping the file i want all the file of a folder as one zip file .

    please let meknow how to achieve this with win zip using excute sql

  • It seems you do not have the public class ScriptMain. Why not?

    Try copy pasting your code into an empty script task, without deleting the class ScriptMain.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • chhaya.porwal (12/14/2011)


    thanks Koen Verbeeck,

    i tried script from given link , but its file by file zipping the file i want all the file of a folder as one zip file .

    please let meknow how to achieve this with win zip using excute sql

    Then use the Execute Process Task like I already suggested.

    Also try Google. There are tons of articles out there that describe how to do it.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • since last 1 week i am googling like any thing 🙁

    i need to do this by the end of this week 🙁

  • As Koen has already mentioned...

    Add a Execute Process Task -->Configure the task

    Example(Add this as arguments):

    rar a <<name_of_rar_file>> <<Path to folder or file fully qualified>>

    This lengthy c# code is simply not required

    Raunak J

  • so mean to say

    i will right

    rar a C:\Program Files\WinRAR c : /excel

  • here's another example using 7zip's command line utility:

    my example here shows my exe in a specific path for clarity.

    http://downloads.sourceforge.net/sevenzip/7za920.zip

    "C:\DataFiles\7zip_CommandLine_7za465\7za.exe" a "C:\DataFiles\myZipFile.zip" "C:\DataFiles\" -y

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • chhaya.porwal (12/14/2011)


    so mean to say

    i will right

    rar a C:\Program Files\WinRAR c : /excel

    <<name of rar file>> = Name of the newly created .rar file(every file has a name) 🙂

    rar a Excel.rar c:\excel

    This is compress all your files found in the folder C:\excel to a VirtualStore(as in Windows7)

    Raunak J

  • Thanks a million guys 🙂

    My problem resolve using excute process task 🙂

  • Glad to know that the issue is solved. But some homework before posting doubts is highly recommended. 🙂

    Raunak J

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply