andruhovski icon

Task-2019-09-06

andruhovski | PRO | 09/06/19 02:11:01 PM UTC | 0 ⭐ | 1048 👁️ | Never ⏰ | []
C# |

3.24 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
 
 
namespace Demo
{
    class Program
    {
        static void Main()
        {
            const string testFile = @"C:\TMP\Demo.zip";
            var runner = new UnZipSolution(@"C:\TMP\2");
            runner.MethodA(testFile);
        }
    }
    class UnZipSolution
    {
        private readonly string _outFolder;
 
        public UnZipSolution(string temporaryDirectory)
        {
            _outFolder = temporaryDirectory;
        }
 
        public void MethodA(string archiveFilenameIn)
        {            
            var files =  ExtractZipFile(archiveFilenameIn,string.Empty, _outFolder);
            if (files.All(MethodB))
            {
                //files.ForEach(File.Delete);
                Directory.Delete(_outFolder, true);
                File.Delete(archiveFilenameIn);
            }
        }
 
        private static bool MethodB(string s)
        {            
            Console.WriteLine("Empty method is Ok!");
            return true;
        }
 
 
        public List<string> ExtractZipFile (string archiveFilenameIn, string password, string outFolder)
        {
            var fileNames = new List<string>();
            ZipFile zf = null;
            try
            {
                var fs = File.OpenRead(archiveFilenameIn);
                zf = new ZipFile(fs);
                if (!string.IsNullOrEmpty(password))
                {
                    zf.Password = password; // AES encrypted entries are handled automatically
                }
                foreach (ZipEntry zipEntry in zf)
                {
                    if (!zipEntry.IsFile) continue; // Ignore directories
 
                    var entryFileName = Path.GetFileName(zipEntry.Name);
                    var s = Path.GetExtension(entryFileName);
                    if (s == null || !s.Equals(".csv")) continue; // Ignore non-csv                    
 
                    var buffer = new byte[4096]; // 4K is optimum
                    var zipStream = zf.GetInputStream(zipEntry);
 
                    // Manipulate the output filename here as desired.
                    var fullZipToPath = Path.Combine(outFolder, entryFileName);
                    var directoryName = Path.GetDirectoryName(fullZipToPath);
                    if (!string.IsNullOrEmpty(directoryName))
                        Directory.CreateDirectory(directoryName);
 
                    using (var streamWriter = File.Create(fullZipToPath))
                    {
                        StreamUtils.Copy(zipStream, streamWriter, buffer);
                    }
                    fileNames.Add(entryFileName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (zf != null)
                {
                    zf.IsStreamOwner = true; // Makes close also shut the underlying stream
                    zf.Close(); // Ensure we release resources
                }
            }
            return fileNames;
        }
    }
}

Comments