by
13
7
3,630
2
Top 1% !
Popular
Famous
Nice
Easy-to-find
Specified
MultiPlatform
Popularity: 3025th place
ClassNameZipShitz
Languagec#
MethodNameZipShit
OutputTypevoid

ZipStich.cs  !!!   dont be a dip,   ZIP n STITCH!!!   NO 3rd party dependancy!   .Net 3.5 and greater

archive just a file or a whole directory.  YEAH!
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/642096/ZipStich-cs--!!!---dont-be-a-dip----ZIP-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System; // usage demo.... public class Program { static void Main(string[] args) { ZipShitz zip = new ZipShitz(@"c:\test.zip", @"c:\TestFolder\"); zip.OnZipDone += zip_OnZipDone; zip.ZipShitAsync(new Action(sayIt)); } static void zip_OnZipDone(System.IO.Packaging.Package pakage, Action callbak) { callbak.Invoke(); } static void sayIt() { Console.WriteLine(""); Console.WriteLine(">> All Files Zipped and Complete! <<"); Console.Read(); } } // end demo.... // using System; using System.IO; using System.IO.Packaging; using System.Text; using System.Threading.Tasks; public class ZipShitz { public delegate void ZipCompleteHandler(Package pakage, Action callbak); public event ZipCompleteHandler OnZipDone; public string ZipName { get; set; } public string TargetFolder { get; set; } public ZipShitz(string zName, string trgt) { OnZipDone += ZipShitz_OnZipDone; ZipName = zName; TargetFolder = trgt; } public ZipShitz() { OnZipDone += ZipShitz_OnZipDone; } void ZipShitz_OnZipDone(Package pakg, Action cbak) { if (!cbak.Equals(null)) { cbak.DynamicInvoke(); } OnZipDone -= ZipShitz_OnZipDone; } public void ZipShit() { using (var pkg = ZipPackage.Open(ZipName, FileMode.Create)) { foreach (var fila in Directory.GetFiles(TargetFolder, "*.*", SearchOption.AllDirectories)) { try { Console.WriteLine("Packing " + fila); Uri relUri = MapRelativePath(fila); var pkgPart = pkg.CreatePart(relUri, System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Maximum); using (var fstrm = new FileStream(fila, FileMode.Open, FileAccess.Read)) { StreamClone(fstrm, pkgPart.GetStream()); } } catch (Exception x) { throw x; } } } } public void ZipShitAsync(Action assbak = null) { Package pkg = null; Task.Factory.StartNew(() => { using (pkg = ZipPackage.Open(ZipName, FileMode.Create)) { foreach (string fila in Directory.GetFiles(TargetFolder, "*.*", SearchOption.AllDirectories)) { try { Console.WriteLine("Packing " + fila); Uri relUri = MapRelativePath(fila); var pkgPart = pkg.CreatePart(relUri, System.Net.Mime.MediaTypeNames.Application.Octet, CompressionOption.Maximum); using (FileStream fstrm = new FileStream(fila, FileMode.Open, FileAccess.Read)) { StreamClone(fstrm, pkgPart.GetStream()); } } catch (Exception x) { x = null; } } } }).Wait(); if (!OnZipDone.Equals(null)) { this.OnZipDone.DynamicInvoke(pkg, assbak); } } void StreamClone(Stream src, Stream tgt) { try { const int bufLen = 16384; byte[] buf = new byte[bufLen]; int bytz = 0; while ((bytz = src.Read(buf, 0, bufLen)) > 0) { tgt.Write(buf, 0, bytz); } } catch (Exception x) { throw x; } } Uri MapRelativePath(string fil) { Uri pth2 = null; try { var rlPth = fil.Substring(fil.IndexOf('\\')).Replace('\\', '/').Replace(' ', '_'); pth2 = new Uri(CleanString(rlPth), UriKind.Relative); } catch (Exception x) { throw x; } return pth2; } string CleanString(string argz) { byte[] bytz = null; try { string nrmz = argz.Normalize(NormalizationForm.FormKD); Encoding ecdr = Encoding.GetEncoding(Encoding.ASCII.CodePage, new EncoderReplacementFallback(""), new DecoderReplacementFallback("")); bytz = ecdr.GetBytes(nrmz); } catch (Exception x) { throw x; } return Encoding.ASCII.GetString(bytz); } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus