I am trying to create a script which can Zip any file on the fly. For this a DLL SharpZipLib i have used. But unable to run the Package. Following code i found on the internet which i have used as is for a sample inside Script Task control.


using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.Windows.Forms; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using ICSharpCode.SharpZipLib.Core; using System.IO;

public void Main() {

        CreateSample("D:\\TestZipResult\\", "D:\\TestZipTarget\\");            

        // TODO: Add your code here
        Dts.TaskResult = (int)ScriptResults.Success;


// Compresses the files in the nominated folder, and creates a zip file on disk named as outPathname. //

    public void CreateSample(string outPathname, string folderName)

        FileStream fsOut = File.Create(outPathname);
        ZipOutputStream zipStream = new ZipOutputStream(fsOut);

        zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

        // This setting will strip the leading part of the folder path in the entries, to
        // make the entries relative to the starting folder.
        // To include the full path for each entry up to the drive root, assign folderOffset = 0.
        int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);

        CompressFolder(folderName, zipStream, folderOffset);

        zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream


    // Recurses down the folder structure
    private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)

        string[] files = Directory.GetFiles(path);

        foreach (string filename in files)

            FileInfo fi = new FileInfo(filename);

            string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
            entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

            // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
            //   newEntry.AESKeySize = 256;

            // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
            // you need to do one of the following: Specify UseZip64.Off, or set the Size.
            // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
            // but the zip will be in Zip64 format which not all utilities can understand.
            //   zipStream.UseZip64 = UseZip64.Off;
            newEntry.Size = fi.Length;


            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
                StreamUtils.Copy(streamReader, zipStream, buffer);
        string[] folders = Directory.GetDirectories(path);
        foreach (string folder in folders)
            CompressFolder(folder, zipStream, folderOffset);



Following is an error i am getting whereas the reference to DLL is properly added.

=========================================== Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Could not load file or assembly 'ICSharpCode.SharpZipLib, Version=, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' or one of its dependencies. The system cannot find the file specified. File name: 'ICSharpCode.SharpZipLib, Version=, Culture=neutral, PublicKeyToken=1b03e6acf1164f73'


I would be grateful if someone could help me out from this.

Regards, F. Ahmed


I am not sure how to do this with SharpZipLib - it looks like there is an issue finding the assembly. Have you tried to GAC the assembly?

That said, this should be an easy task with the SSIS compression tasks at http://www.nsoftware.com/ssis/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.