Advertisement
andruhovski

Task-2019-09-06

Sep 6th, 2019
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using ICSharpCode.SharpZipLib.Core;
  6. using ICSharpCode.SharpZipLib.Zip;
  7.  
  8.  
  9. namespace Demo
  10. {
  11.     class Program
  12.     {
  13.         static void Main()
  14.         {
  15.             const string testFile = @"C:\TMP\Demo.zip";
  16.             var runner = new UnZipSolution(@"C:\TMP\2");
  17.             runner.MethodA(testFile);
  18.         }
  19.     }
  20.     class UnZipSolution
  21.     {
  22.         private readonly string _outFolder;
  23.  
  24.         public UnZipSolution(string temporaryDirectory)
  25.         {
  26.             _outFolder = temporaryDirectory;
  27.         }
  28.  
  29.         public void MethodA(string archiveFilenameIn)
  30.         {            
  31.             var files =  ExtractZipFile(archiveFilenameIn,string.Empty, _outFolder);
  32.             if (files.All(MethodB))
  33.             {
  34.                 //files.ForEach(File.Delete);
  35.                 Directory.Delete(_outFolder, true);
  36.                 File.Delete(archiveFilenameIn);
  37.             }
  38.         }
  39.  
  40.         private static bool MethodB(string s)
  41.         {            
  42.             Console.WriteLine("Empty method is Ok!");
  43.             return true;
  44.         }
  45.  
  46.  
  47.         public List<string> ExtractZipFile (string archiveFilenameIn, string password, string outFolder)
  48.         {
  49.             var fileNames = new List<string>();
  50.             ZipFile zf = null;
  51.             try
  52.             {
  53.                 var fs = File.OpenRead(archiveFilenameIn);
  54.                 zf = new ZipFile(fs);
  55.                 if (!string.IsNullOrEmpty(password))
  56.                 {
  57.                     zf.Password = password; // AES encrypted entries are handled automatically
  58.                 }
  59.                 foreach (ZipEntry zipEntry in zf)
  60.                 {
  61.                     if (!zipEntry.IsFile) continue; // Ignore directories
  62.  
  63.                     var entryFileName = Path.GetFileName(zipEntry.Name);
  64.                     var s = Path.GetExtension(entryFileName);
  65.                     if (s == null || !s.Equals(".csv")) continue; // Ignore non-csv                    
  66.  
  67.                     var buffer = new byte[4096]; // 4K is optimum
  68.                     var zipStream = zf.GetInputStream(zipEntry);
  69.  
  70.                     // Manipulate the output filename here as desired.
  71.                     var fullZipToPath = Path.Combine(outFolder, entryFileName);
  72.                     var directoryName = Path.GetDirectoryName(fullZipToPath);
  73.                     if (!string.IsNullOrEmpty(directoryName))
  74.                         Directory.CreateDirectory(directoryName);
  75.  
  76.                     using (var streamWriter = File.Create(fullZipToPath))
  77.                     {
  78.                         StreamUtils.Copy(zipStream, streamWriter, buffer);
  79.                     }
  80.                     fileNames.Add(entryFileName);
  81.                 }
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 Console.WriteLine(ex.Message);
  86.             }
  87.             finally
  88.             {
  89.                 if (zf != null)
  90.                 {
  91.                     zf.IsStreamOwner = true; // Makes close also shut the underlying stream
  92.                     zf.Close(); // Ensure we release resources
  93.                 }
  94.             }
  95.             return fileNames;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement