/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Threading.Tasks; namespace wasSharp { public static class IO { /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Combine multiple paths. /// /// an array of paths /// a combined path public static string PathCombine(params string[] paths) { return paths.Aggregate((x, y) => Path.Combine(x, y)); } /// /// Strip characters that are incompatible with file names. /// /// the name of the file /// a clean string private static string CleanFileName(string fileName) { return Path.GetInvalidFileNameChars() .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); } /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Compress a stream using Deflate compression. /// /// the stream to compress /// the stream to write the compressed stream to /// whether to leave the compression stream open /// an awaitable Task public static async Task DeflateCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream, bool leaveOpen = false) { using ( var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen)) { await uncompressedSourceStream.CopyToAsync(compressionStream); } } /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Decompress a Deflate-compressed stream. /// /// the compressed stream to decompress /// the stream to write the decompressed stream to /// whether to leave the stream open after decompression /// an awaitable Task public static async Task DeflateDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream, bool leaveOpen = false) { using ( var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen)) { await decompressionStream.CopyToAsync(uncompressedDestinationStream); } } /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Compress a stream using GZip compression. /// /// the stream to compress /// the stream to write the compressed stream to /// whether to leave the stream open after compressing /// an awaitable Task public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream, bool leaveOpen = false) { using ( var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen) ) { await uncompressedSourceStream.CopyToAsync(compressionStream); } } /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Decompress a GZiped stream. /// /// the stream to decompress /// the stream to write the decompressed stream to /// whether the decompression stream should be left open /// an awaitable Task public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream, bool leaveOpen = false) { using ( var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen)) { await decompressionStream.CopyToAsync(uncompressedDestinationStream); } } /////////////////////////////////////////////////////////////////////////// // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // /////////////////////////////////////////////////////////////////////////// /// /// Splits a path using a separator and an escape character. /// /// the path to split /// the separator character /// the escape character /// true if the initial separator should be included /// path parts public static IEnumerable PathSplit(this string path, char separator, char? escape = null, bool includeRootSeparator = true) { var s = new Stack(); var p = new StringBuilder(); foreach (var c in path) { if (c.Equals(escape)) { s.Push(c); continue; } if (c.Equals(separator)) { if (s.Count().Equals(0) || !s.Peek().Equals(escape)) { if (p.Length.Equals(0) && includeRootSeparator) { p.Append(c); continue; } yield return p.ToString(); p = new StringBuilder(); continue; } s.Pop(); while (!s.Count().Equals(0)) { p.Append(s.Pop()); } p.Append(c); continue; } p.Append(c); } while (!s.Count().Equals(0)) { p.Append(s.Pop()); } yield return p.ToString(); } } }