ALCO135
ALCO135

Reputation: 1

Why with Script task in SSIS i can't unzip exactly the path like winzip or 7zip?

I'm using Script task with Microsoft Visual C# 2012 in SSIS.

I want to unzip 20160423.zip which have inside a test folder and inside of the test folder have insert, delete and update folders and in each folder have .dat files:

20160423.zip:

I want to unzip like this:

This is the hierarchy

The Script Task looks like

public void Main()
{
    string zipfullpath = Dts.Variables["User::ZipFullPath"].Value.ToString();
    string inputfolder = Dts.Variables["$Project::InputFolder"].Value.ToString();

    using (ZipArchive arch = ZipFile.OpenRead(zipfullpath))
    {
        foreach (ZipArchiveEntry entry in arch.Entries)
        {
            entry.ExtractToFile(Path.Combine(inputfolder, entry.FullName),true);
            //ZipFile.ExtractToDirectory(zipfullpath, inputfolder);
        }
    }
    //File.Delete(zipfullpath);
    // TODO: Add your code here

    Dts.TaskResult = (int)ScriptResults.Success; 
}

UPDATE:

I did some changes in the code and now i can create the main/root folder i.e. 20160423 but I can't create the subdirectories (Insert, Delete, Update) to extract the .dat files in each folders. When i tried to run the code the debug prompt an error that the path don't exist because the folder insert, update and delete.

public void Main()
{
    // TODO: Add your code here

    string zipfullpath = Dts.Variables["User::ZipFullPath"].Value.ToString();
    string inputfolder = Dts.Variables["$Project::InputFolder"].Value.ToString();
    string rootfolder = Path.GetFileNameWithoutExtension(zipfullpath);

    using (ZipArchive arch = ZipFile.OpenRead(zipfullpath))
    {
        //var root = arch.Entries[0].FullName;
        //var result = from curr in arch.Entries
        //             where Path.GetDirectoryName(curr.FullName) != root
        //             where !string.IsNullOrEmpty(curr.Name)
        //             select curr;

        foreach (ZipArchiveEntry entry in arch.Entries)
        {
            //entry.ExtractToFile(Path.Combine(inputfolder, entry.FullName),true);
            //ZipFile.ExtractToDirectory(zipfullpath, inputfolder);
            //Gets the complete path for the destination file, including any
            //relative paths that were in the zip file
            string destinationFileName = Path.Combine(inputfolder,rootfolder, entry.FullName);

            //Gets just the new path, minus the file name so we can create the
            //directory if it does not exist
            string destinationFilePath = Path.GetDirectoryName(destinationFileName);

            //var newName = entry.FullName.Substring(root.Length);

            //Creates the directory (if it doesn't exist) for the new path
            Directory.CreateDirectory(destinationFileName);


            //if (!File.Exists(destinationFileName) || File.GetLastWriteTime(destinationFileName) < entry.LastWriteTime)
            //{
                //Either the file didn't exist or this file is newer, so
                //we will extract it and overwrite any existing file
                entry.ExtractToFile(destinationFileName, true);
            //}

        }
    }
    //File.Delete(zipfullpath);

    Dts.TaskResult = (int)ScriptResults.Success;
}

Upvotes: 0

Views: 878

Answers (1)

Felix
Felix

Reputation: 203

Why not just use 7zip. Have a look at http://sqlserversolutions.blogspot.de/2008/10/zip-and-unzip-files-in-folder.html

regards

Upvotes: 0

Related Questions