public void DelayedHeaderWriteFlushWithData() { var ms = new MemoryStream(); Assert.AreEqual(0, ms.Length); using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) { Assert.AreEqual(0, ms.Length); // #382 - test flushing the stream before writing to it. outStream.Flush(); outStream.WriteByte(45); } ms.Seek(0, SeekOrigin.Begin); // Test that the gzip stream can be read var readStream = new MemoryStream(); using (GZipInputStream inStream = new GZipInputStream(ms)) { inStream.CopyTo(readStream); } // Check that the data was read byte[] data = readStream.ToArray(); CollectionAssert.AreEqual(new byte[] { 45 }, data, "Decompressed data should match initial data"); }
public void OutputStreamOwnership() { var memStream = new TrackedMemoryStream(); var s = new GZipOutputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.Close(); Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close"); Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); memStream = new TrackedMemoryStream(); s = new GZipOutputStream(memStream); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); s.IsStreamOwner = false; s.Close(); Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); }
public static byte[] Compress(byte[] bytesToCompress) { byte[] rebyte = null; MemoryStream ms = new MemoryStream(); GZipOutputStream s = new GZipOutputStream(ms); try { s.Write(bytesToCompress, 0, bytesToCompress.Length); s.Flush(); s.Finish(); } catch (System.Exception ex) { #if UNITY_EDITOR Debug.Log(ex); #endif } ms.Seek(0, SeekOrigin.Begin); rebyte = ms.ToArray(); s.Close(); ms.Close(); s.Dispose(); ms.Dispose(); return(rebyte); }
public static void SaveToFile(KPTranslation kpTrl, string strFileName) { FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None); #if !KeePassLibSD GZipStream gz = new GZipStream(fs, CompressionMode.Compress); #else GZipOutputStream gz = new GZipOutputStream(fs); #endif XmlWriterSettings xws = new XmlWriterSettings(); xws.CheckCharacters = true; xws.Encoding = Encoding.UTF8; xws.Indent = true; xws.IndentChars = "\t"; XmlWriter xw = XmlWriter.Create(gz, xws); XmlSerializer xmlSerial = new XmlSerializer(typeof(KPTranslation)); xmlSerial.Serialize(xw, kpTrl); xw.Close(); gz.Close(); fs.Close(); }
public void TestGZip() { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); ms.Seek(0, SeekOrigin.Begin); GZipInputStream inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
/// <summary> /// 压缩数据。 /// </summary> /// <param name="stream">要压缩的数据的二进制流。</param> /// <param name="compressedStream">压缩后的数据的二进制流。</param> /// <returns>是否压缩数据成功。</returns> public bool Compress(Stream stream, Stream compressedStream) { if (stream == null) { return(false); } if (compressedStream == null) { return(false); } try { GZipOutputStream gZipOutputStream = new GZipOutputStream(compressedStream); int bytesRead = 0; while ((bytesRead = stream.Read(m_CachedBytes, 0, CachedBytesLength)) > 0) { gZipOutputStream.Write(m_CachedBytes, 0, bytesRead); } gZipOutputStream.Finish(); ProcessHeader(compressedStream); return(true); } catch { return(false); } finally { Array.Clear(m_CachedBytes, 0, CachedBytesLength); } }
private void CreateTarGZ(string tgzFilename, string sourceDirectory) { try { Stream outStream = File.Create(tgzFilename); Stream gzoStream = new GZipOutputStream(outStream); TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzoStream); // Note that the RootPath is currently case sensitive and must be forward slashes e.g. "c:/temp" // and must not end with a slash, otherwise cuts off first char of filename // This is scheduled for fix in next release tarArchive.RootPath = sourceDirectory.Replace('\\', '/'); if (tarArchive.RootPath.EndsWith("/")) { tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1); } AddDirectoryFilesToTar(tarArchive, sourceDirectory, true); tarArchive.Close(); } catch (SystemException se) { MessageBox.Show(se.Message); } }
///-------------------------------------------------------------------------------------------------------- /// public bool SetPassword(string pwd) { string hdmasterkeyid = QtumHandler.GetHDMasterKeyId(); if (string.IsNullOrEmpty(hdmasterkeyid)) { Logger.Log("퀀텀 지갑의 구동 상태를 확인 해 주세요."); return(false); } try { byte[] pwdBytes = Encoding.UTF8.GetBytes(Encrypt(pwd, Encrypt(hdmasterkeyid, Config.RPCPassword))); using (FileStream fs = File.Open(passwordFile, FileMode.Create)) { using (GZipOutputStream gzs = new GZipOutputStream(fs)) { using (MemoryStream c = new MemoryStream()) { gzs.Write(pwdBytes, 0, pwdBytes.Length); } } } } catch (Exception e) { Logger.Log(e.ToString()); return(false); } return(true); }
// Sadico's Gzip Compression (but my comments) public static byte[] GzipData(float[] data) { if (data == null) { return(null); } using (Stream memOutput = new MemoryStream()) { using (GZipOutputStream zipOut = new GZipOutputStream(memOutput)) { using (BinaryWriter writer = new BinaryWriter(zipOut)) { byte[] floatSerialization = new byte[data.Length * 4]; // Float uses 4 bytes (it's 32 bit) Buffer.BlockCopy(data, 0, floatSerialization, 0, floatSerialization.Length); // Serializes the float[] to bytes writer.Write(floatSerialization); // Writes the bytes to the stream writer.Flush(); // Clears the buffer for memory cleanup zipOut.Finish(); // Finishes the stream // Stores the data into a field to return byte[] bytes = new byte[memOutput.Length]; memOutput.Seek(0, SeekOrigin.Begin); memOutput.Read(bytes, 0, bytes.Length); return(bytes); } } } }
public byte[] Compress(byte[] buffer, long index, long count) { byte[] arrBuffer = null; MemoryStream ms = new MemoryStream(); GZipOutputStream objGzip = new GZipOutputStream(ms); #region copy //const int BUFFER_SIZE = 1024 * 10; //byte[] arrBuffer = new byte[BUFFER_SIZE]; //int nGetedCount = 0; //do //{ // nGetedCount = ms.Read(arrBuffer, 0, BUFFER_SIZE); // objGzip.Write(arrBuffer, 0, nGetedCount); //} while (nGetedCount > 0); #endregion objGzip.Write(buffer, 0, buffer.Length); //objGzip.SetLevel(level); objGzip.Finish(); arrBuffer = ms.ToArray(); ms.Close(); objGzip.Close(); return(arrBuffer); }
public static void ObjectToStream <TItem>(TItem obj, Stream stream, SerializationMode mode, bool compress) { if (mode == SerializationMode.Json) { if (compress) { using (var outZStream = new GZipOutputStream(stream) { IsStreamOwner = false }) { var writer = new JsonTextWriter(new StreamWriter(outZStream)); Serializer.Serialize(writer, obj); writer.Flush(); } } else { var writer = new JsonTextWriter(new StreamWriter(stream)); Serializer.Serialize(writer, obj); writer.Flush(); } } else { ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, obj, PrefixStyle.Fixed32); } }
/// <summary> /// files to gzip /// </summary> /// <param name="tgzFileName">tgzFileName.</param> /// <param name="files">files.</param> /// <param name="rootPath">rootPath.</param> public static void CreateGZip(string tgzFileName, List <String> files, string rootPath = null) { if (string.IsNullOrEmpty(rootPath)) { rootPath = CUtils.realStreamingAssetsPath; } rootPath = rootPath.Replace('\\', '/'); if (rootPath.EndsWith("/")) { rootPath = rootPath.Remove(rootPath.Length - 1); } if (File.Exists(tgzFileName)) { File.Delete(tgzFileName); } using (Stream outStream = File.Create(tgzFileName)) { using (GZipOutputStream gzoStream = new GZipOutputStream(outStream)) { TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzoStream); tarArchive.RootPath = rootPath; foreach (string filename in files) { //check folder AddDirectoryFilesToTar(tarArchive, rootPath, filename); } } } }
private void CreateTarArchive() { log.Trace("TAR (Create): '{0}' -> '{1}'", directory, tarFile); if (File.Exists(tarFile)) { File.Delete(tarFile); } using (var fs = File.Open(tarFile, FileMode.CreateNew, FileAccess.Write, FileShare.None)) { using (var gzipStream = new GZipOutputStream(fs)) { using (var tarArchive = TarArchive.CreateOutputTarArchive(gzipStream)) { tarArchive.RootPath = directory.Replace('\\', '/'); if (tarArchive.RootPath.EndsWith("/")) { tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1); } var tarEntry = TarEntry.CreateEntryFromFile(directory); tarArchive.WriteEntry(tarEntry, true); log.Debug("TAR DIRECTORY {0}", directory); log.Debug("TAR ROOT {0}", tarArchive.RootPath); } } } }
public void OriginalFilename() { var content = "FileContents"; using (var ms = new MemoryStream()) { using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) { outStream.FileName = "/path/to/file.ext"; var writeBuffer = Encoding.ASCII.GetBytes(content); outStream.Write(writeBuffer, 0, writeBuffer.Length); outStream.Flush(); outStream.Finish(); } ms.Seek(0, SeekOrigin.Begin); using (var inStream = new GZipInputStream(ms)) { var readBuffer = new byte[content.Length]; inStream.Read(readBuffer, 0, readBuffer.Length); Assert.AreEqual(content, Encoding.ASCII.GetString(readBuffer)); Assert.AreEqual("file.ext", inStream.GetFilename()); } } }
public static void SaveToFile(KPTranslation kpTrl, string strFileName, IXmlSerializerEx xs) { if (xs == null) { throw new ArgumentNullException("xs"); } FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None); #if !KeePassLibSD GZipStream gz = new GZipStream(fs, CompressionMode.Compress); #else GZipOutputStream gz = new GZipOutputStream(fs); #endif XmlWriterSettings xws = new XmlWriterSettings(); xws.CheckCharacters = true; xws.Encoding = StrUtil.Utf8; xws.Indent = true; xws.IndentChars = "\t"; XmlWriter xw = XmlWriter.Create(gz, xws); xs.Serialize(xw, kpTrl); xw.Close(); gz.Close(); fs.Close(); }
public static void Save(KPTranslation kpTrl, Stream sOut, IXmlSerializerEx xs) { if (xs == null) { throw new ArgumentNullException("xs"); } #if !KeePassLibSD GZipStream gz = new GZipStream(sOut, CompressionMode.Compress); #else GZipOutputStream gz = new GZipOutputStream(sOut); #endif XmlWriterSettings xws = new XmlWriterSettings(); xws.CheckCharacters = true; xws.Encoding = StrUtil.Utf8; xws.Indent = true; xws.IndentChars = "\t"; XmlWriter xw = XmlWriter.Create(gz, xws); xs.Serialize(xw, kpTrl); xw.Close(); gz.Close(); sOut.Close(); }
public void Save(string file) { using var fileStream = (new FileInfo(file)).OpenWrite(); using var gzipStream = new GZipOutputStream(fileStream); using var memoryStream = new MemoryStream(Bytes); memoryStream.CopyTo(gzipStream); }
private void CreateOrModifyCacheFile(BinaryData cacheBinary, bool compress) { SN.File f = null; MemoryStream cacheStream = new MemoryStream(); if (compress) { GZipOutputStream gzipStream = new GZipOutputStream(cacheStream); byte[] buff = Encoding.ASCII.GetBytes(this._content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Flush(); gzipStream.Close(); // set compressed binary byte[] compressedData = cacheStream.ToArray(); cacheBinary.SetStream(new MemoryStream(compressedData)); } else { cacheBinary.SetStream(Tools.GetStreamFromString(_content)); } // gets cache file or creates a new one, the new stream will be saved in both cases if (!Node.Exists(FullCacheFilePath)) { f = SN.File.CreateByBinary(this.CacheFolder, cacheBinary); f.Name = _cacheFile; } else { f = Node.Load <SN.File>(this.FullCacheFilePath); f.Binary = cacheBinary; } f.Save(); }
public static void Main(string[] args) { if (args[0] == "-d") // decompress { Stream s = new GZipInputStream(File.OpenRead(args[1])); FileStream fs = File.Create(Path.GetFileNameWithoutExtension(args[1])); int size = 2048; byte[] writeData = new byte[2048]; while (true) { size = s.Read(writeData, 0, size); if (size > 0) { fs.Write(writeData, 0, size); } else { break; } } s.Close(); } else // compress { Stream s = new GZipOutputStream(File.Create(args[0] + ".gz")); FileStream fs = File.OpenRead(args[0]); byte[] writeData = new byte[fs.Length]; fs.Read(writeData, 0, (int)fs.Length); s.Write(writeData, 0, writeData.Length); s.Close(); } }
/// <summary> /// 压缩数据。 /// </summary> /// <param name="bytes">要压缩的数据的二进制流。</param> /// <param name="offset">要压缩的数据的二进制流的偏移。</param> /// <param name="length">要压缩的数据的二进制流的长度。</param> /// <param name="compressedStream">压缩后的数据的二进制流。</param> /// <returns>是否压缩数据成功。</returns> public bool Compress(byte[] bytes, int offset, int length, Stream compressedStream) { if (bytes == null) { return(false); } if (offset < 0 || length < 0 || offset + length > bytes.Length) { return(false); } if (compressedStream == null) { return(false); } try { GZipOutputStream gZipOutputStream = new GZipOutputStream(compressedStream); gZipOutputStream.Write(bytes, offset, length); gZipOutputStream.Finish(); ProcessHeader(compressedStream); return(true); } catch { return(false); } }
public TextWriter GetWriter(Uri uri) { string local_path = LookupPathRaw(uri); // LookupPathRaw returns local path, if NOT self_cached if (local_path == SELF_CACHE_TAG) { throw new ArgumentException("uri", String.Format("Cannot return TextCache writer for self-cached file {0}", uri)); } // If there is no path, create new prospective path, by called Guid.NewGuid() if (local_path == null || local_path == BLOB_TAG) { string guid = Guid.NewGuid().ToString(); local_path = Path.Combine(guid.Substring(0, 2), guid.Substring(2)); } // Return TextCacheWriteStream (Uri, path, handler) Stream stream; stream = new TextCacheWriteStream(Path.Combine(text_cache_dir, local_path), world_readable, delegate(byte[] buffer) { CachedDataWriteFinished(uri, local_path, buffer); }); // But after wrapping it with compression stream = new GZipOutputStream(stream); StreamWriter writer; writer = new StreamWriter(new BufferedStream(stream)); return(writer); }
private static string EncodeBase64(byte[] input) { using (var compressedStream = new MemoryStream()) { // mono requires an installed zlib library for GZipStream to work :( // using (Stream csStream = new GZipStream(compressedStream, CompressionMode.Compress)) using (Stream csStream = new GZipOutputStream(compressedStream)) { csStream.Write(input, 0, input.Length); } string returnValue = Convert.ToBase64String(compressedStream.ToArray()); // Added the following to fix issue #429: Base64 content can include the slash character '/', and // if it happens to have two of them contiguously, it forms a comment in the persistence file and // truncates the value. So change them to a different character to protect the file. // The comma ',' char is not used by base64 so it's a safe alternative to use as we'll be able to // swap all of the commas back to slashes on reading, knowing that commas can only appear as the // result of this swap on writing: returnValue = returnValue.Replace('/', ','); SafeHouse.Logger.SuperVerbose("About to store the following Base64 string:\n" + returnValue); return(returnValue); } }
public void DelayedHeaderWriteFlushNoData() { var ms = new MemoryStream(); Assert.AreEqual(0, ms.Length); using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) { // #382 - test flushing the stream before writing to it. outStream.Flush(); } ms.Seek(0, SeekOrigin.Begin); // Test that the gzip stream can be read var readStream = new MemoryStream(); using (GZipInputStream inStream = new GZipInputStream(ms)) { inStream.CopyTo(readStream); } byte[] data = readStream.ToArray(); Assert.That(data, Is.Empty, "Should not have any decompressed data"); }
public void Save(string path, int build, string map, float totalTime) { this.data.Map = string.IsNullOrEmpty(map) ? null : Path.GetFileNameWithoutExtension(map); this.data.TotalTime = totalTime; this.data.Map = map; this.data.UUID = this.main.Settings.UUID; Point screenSize; #if VR this.data.VR = this.main.VR; if (this.main.VR) { screenSize = this.main.VRActualScreenSize; } else #endif screenSize = this.main.ScreenSize; this.data.ScreenSize = screenSize; this.data.IsFullscreen = this.main.Settings.Fullscreen; string filename = string.Format("{0}-{1}-{2}.xml.gz", build, this.data.Map == null ? "null" : this.data.Map, this.data.ID); using (Stream fs = new FileStream(Path.Combine(path, filename), FileMode.Create, FileAccess.Write, FileShare.None)) using (Stream stream = new GZipOutputStream(fs)) new XmlSerializer(typeof(Session)).Serialize(stream, this.data); }
/* KMP message data format * Uncompressed data: [bool-false : data] * Compressed data: [bool-true : Int32-uncompressed length : compressed_data] */ public static byte[] Compress(byte[] data, bool forceUncompressed = false) { if (data == null) { return(null); } byte[] compressedData = null; MemoryStream ms = null; GZipOutputStream gzip = null; try { ms = new MemoryStream(); if (data.Length < MESSAGE_COMPRESSION_THRESHOLD || forceUncompressed) { //Small message, don't compress using (BinaryWriter writer = new BinaryWriter(ms)) { writer.Write(false); writer.Write(data, 0, data.Length); compressedData = ms.ToArray(); ms.Close(); writer.Close(); } } else { //Compression enabled Int32 size = data.Length; using (BinaryWriter writer = new BinaryWriter(ms)) { writer.Write(true); writer.Write(size); gzip = new GZipOutputStream(ms); gzip.Write(data, 0, data.Length); gzip.Close(); compressedData = ms.ToArray(); ms.Close(); writer.Close(); } } } catch { return(null); } finally { if (gzip != null) { gzip.Dispose(); } if (ms != null) { ms.Dispose(); } } return(compressedData); }
public static void Compress(string directory, string command, string destination) { if (!Directory.Exists(directory)) { return; } var filesDirectory = Path.Combine(directory, command + "-files"); var file = destination + ".oipkg"; if (Environment.OSVersion.Platform == PlatformID.MacOSX || Environment.OSVersion.Platform == PlatformID.Unix) { var args = new StringBuilder(); args.Append("\"" + file + "\" "); Directory .GetFiles(directory, command + ".*") .ToList() .ForEach(x => args.Append("\"" + toRelative(x, directory) + "\" ")); args.Append("\"" + toRelative(filesDirectory, directory) + "\""); run(directory, "tar", "-czf " + args.ToString()); return; } var currentDirectory = Directory.GetCurrentDirectory(); Directory.SetCurrentDirectory(directory); try { var outStream = File.Create(file); var gzoStream = new GZipOutputStream(outStream); var tarArchive = TarArchive.CreateOutputTarArchive(gzoStream); // Note that the RootPath is currently case sensitive and must be forward slashes e.g. "c:/temp" // and must not end with a slash, otherwise cuts off first char of filename // This is scheduled for fix in next release tarArchive.RootPath = directory.Replace('\\', '/'); if (tarArchive.RootPath.EndsWith("/")) { tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1); } Directory .GetFiles(directory, command + ".*") .ToList() .ForEach(x => addFile(tarArchive, x)); addDirectory(tarArchive, filesDirectory); tarArchive.Close(); } catch (Exception ex) { Console.WriteLine("error|Exception during processing {0}", ex); throw; } finally { Directory.SetCurrentDirectory(currentDirectory); } }
static void Build() { using (FileStream file = new FileStream("solodata.dat", FileMode.Create, FileAccess.ReadWrite)) { using (TextReader input = new StreamReader(File.OpenRead("Menus.csv"), Encoding.UTF8)) { using (CsvReader csv = new CsvReader(input)) { file.WriteUInt(0); uint rowCount = 0; csv.Read(); while (csv.Read()) { string value = csv.GetField(1); file.WriteUShort(ushort.Parse(value, System.Globalization.NumberStyles.HexNumber)); rowCount++; } long filePosition = file.Position; file.Position = 0; file.WriteUInt(rowCount); file.Position = filePosition; } } ImportCSV("Cars\\Cars.csv"); var filenames = Directory.EnumerateFiles("Cars", "*.csv").Where(csv => Path.GetFileName(csv) != "Cars.csv"); foreach (string filename in filenames) { ImportCSV(filename); } long startPosition = file.Position; file.WriteUInt(0); foreach (var car in Cars) { file.WriteUInt(CarNameConversion.ToCarID(car.Key)); file.WriteUInt(car.Value); } file.Position = startPosition; file.WriteUInt((uint)Cars.Count); file.Position = 0; using (var gzip = new FileStream("solodata.dat.gz", FileMode.Create, FileAccess.Write)) { using (var compression = new GZipOutputStream(gzip)) { compression.SetLevel(8); compression.IsStreamOwner = false; file.CopyTo(compression); } } } }
private static void Compress(string outputFile, string tempPath) { using var stream = new FileStream(outputFile, FileMode.CreateNew); using var zipStream = new GZipOutputStream(stream); using var archive = TarArchive.CreateOutputTarArchive(zipStream); archive.RootPath = tempPath; archive.AddFilesRecursive(tempPath); }
public override void Close() { GZipOutputStream gzip = new GZipOutputStream(sink); WriteTo(gzip); gzip.Close(); base.Close(); }
private static ArchiveOutputStream CreateTgzStream(System.IO.Stream stream) { GZipOutputStream gzipStream = new GZipOutputStream(stream); TarOutputStream tarStream = new TarOutputStream(gzipStream); gzipStream.IsStreamOwner = false; return(new ArchiveOutputStream(tarStream)); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="response"></param> public void Compress(HttpRequest request, HttpResponse response) { Encoding encoding = Encoding.GetEncoding("windows-1252"); string enc, cacheFile = null, cacheKey = null, content = ""; StringWriter writer = new StringWriter(); byte[] buff = new byte[1024]; GZipOutputStream gzipStream; bool supportsGzip; // Set response headers response.ContentType = "text/javascript"; response.Charset = this.charset; response.Buffer = false; // Setup cache response.Cache.SetExpires(DateTime.Now.AddSeconds(this.ExpiresOffset)); // Check if it supports gzip enc = Regex.Replace("" + request.Headers["Accept-Encoding"], @"\s+", "").ToLower(); supportsGzip = enc.IndexOf("gzip") != -1 || request.Headers["---------------"] != null; enc = enc.IndexOf("x-gzip") != -1 ? "x-gzip" : "gzip"; // Setup cache info if (this.diskCache) { cacheKey = ""; foreach (JSCompressItem item in this.items) { // Get last mod if (item.Type == JSItemType.File) { DateTime fileMod = File.GetLastWriteTime(request.MapPath(item.Value)); if (fileMod > this.lastUpdate) this.lastUpdate = fileMod; } cacheKey += item.Value; } cacheKey = this.cacheFileName != null ? this.cacheFileName : MD5(cacheKey); if (this.gzipCompress) cacheFile = request.MapPath(this.cacheDir + "/" + cacheKey + ".gz"); else cacheFile = request.MapPath(this.cacheDir + "/" + cacheKey + ".js"); } // Use cached file disk cache if (this.diskCache && supportsGzip && File.Exists(cacheFile) && this.lastUpdate == File.GetLastWriteTime(cacheFile)) { if (this.gzipCompress) response.AppendHeader("Content-Encoding", enc); response.WriteFile(cacheFile); return; } foreach (JSCompressItem item in this.items) { if (item.Type == JSItemType.File) { if (!File.Exists(request.MapPath(item.Value))) { writer.WriteLine("alert('Could not load file: " + StringUtils.Escape(item.Value) + "');"); continue; } StreamReader reader = new StreamReader(File.OpenRead(request.MapPath(item.Value)), System.Text.Encoding.UTF8); if (item.RemoveWhiteSpace) { JavaScriptMinifier jsMin = new JavaScriptMinifier(reader, writer); jsMin.Compress(); } else { writer.Write('\n'); writer.Write(reader.ReadToEnd()); writer.Write(";\n"); } reader.Close(); } else { if (item.RemoveWhiteSpace) { JavaScriptMinifier jsMin = new JavaScriptMinifier(new StringReader(item.Value), writer); jsMin.Compress(); } else { writer.Write('\n'); writer.Write(item.Value); writer.Write('\n'); } } } content = writer.ToString(); // Generate GZIP'd content if (supportsGzip) { if (this.gzipCompress) response.AppendHeader("Content-Encoding", enc); if (this.diskCache && cacheKey != null) { try { // Gzip compress if (this.gzipCompress) { gzipStream = new GZipOutputStream(File.Create(cacheFile)); buff = encoding.GetBytes(content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Close(); File.SetLastWriteTime(cacheFile, this.lastUpdate); } else { StreamWriter sw = File.CreateText(cacheFile); sw.Write(content); sw.Close(); File.SetLastWriteTime(cacheFile, this.lastUpdate); } // Write to stream response.WriteFile(cacheFile); } catch (Exception) { content = "/* Not cached */" + content; if (this.gzipCompress) { gzipStream = new GZipOutputStream(response.OutputStream); buff = encoding.GetBytes(content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Close(); } else { response.Write(content); } } } else { content = "/* Not cached */" + content; gzipStream = new GZipOutputStream(response.OutputStream); buff = encoding.GetBytes(content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Close(); } } else { content = "/* Not cached */" + content; response.Write(content); } }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="response"></param> public void Compress(HttpRequest request, HttpResponse response) { Encoding encoding = Encoding.GetEncoding("windows-1252"); string enc, cacheFile = null, cacheKey = null, content = ""; StringWriter writer = new StringWriter(); byte[] buff = new byte[1024]; GZipOutputStream gzipStream; bool supportsGzip; // Set response headers response.ContentType = "text/css"; response.Charset = this.charset; response.Buffer = false; // Setup cache response.Cache.SetExpires(DateTime.Now.AddSeconds(this.ExpiresOffset)); // Check if it supports gzip enc = Regex.Replace("" + request.Headers["Accept-Encoding"], @"\s+", "").ToLower(); supportsGzip = enc.IndexOf("gzip") != -1 || request.Headers["---------------"] != null; enc = enc.IndexOf("x-gzip") != -1 ? "x-gzip" : "gzip"; // Setup cache info if (this.diskCache) { cacheKey = ""; foreach (CSSCompressItem item in this.items) { // Get last mod if (item.Type == CSSItemType.File) { DateTime fileMod = File.GetLastWriteTime(request.MapPath(item.Value)); if (fileMod > this.lastUpdate) this.lastUpdate = fileMod; } cacheKey += item.Value; } cacheKey = this.cacheFileName != null ? this.cacheFileName : MD5(cacheKey); if (this.gzipCompress) cacheFile = request.MapPath(this.cacheDir + "/" + cacheKey + ".gz"); else cacheFile = request.MapPath(this.cacheDir + "/" + cacheKey + ".css"); } // Use cached file disk cache if (this.diskCache && supportsGzip && File.Exists(cacheFile) && this.lastUpdate == File.GetLastWriteTime(cacheFile)) { if (this.gzipCompress) response.AppendHeader("Content-Encoding", enc); response.WriteFile(cacheFile); return; } foreach (CSSCompressItem item in this.items) { if (item.Type == CSSItemType.File) { StreamReader reader = new StreamReader(File.OpenRead(request.MapPath(item.Value)), System.Text.Encoding.UTF8); string data; if (item.RemoveWhiteSpace) data = this.TrimWhiteSpace(reader.ReadToEnd()); else data = reader.ReadToEnd(); if (this.convertUrls) data = Regex.Replace(data, "url\\(['\"]?(?!\\/|http)", "$0" + PathUtils.ToUnixPath(Path.GetDirectoryName(item.Value)) + "/"); writer.Write(data); reader.Close(); } else { if (item.RemoveWhiteSpace) writer.Write(this.TrimWhiteSpace(item.Value)); else writer.Write(item.Value); } } content = writer.ToString(); // Generate GZIP'd content if (supportsGzip) { if (this.gzipCompress) response.AppendHeader("Content-Encoding", enc); if (this.diskCache && cacheKey != null) { try { // Gzip compress if (this.gzipCompress) { gzipStream = new GZipOutputStream(File.Create(cacheFile)); buff = encoding.GetBytes(content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Close(); File.SetLastWriteTime(cacheFile, this.lastUpdate); } else { StreamWriter sw = File.CreateText(cacheFile); sw.Write(content); sw.Close(); File.SetLastWriteTime(cacheFile, this.lastUpdate); } // Write to stream response.WriteFile(cacheFile); } catch (Exception) { content = "/* Not cached */" + content; if (this.gzipCompress) { gzipStream = new GZipOutputStream(response.OutputStream); buff = encoding.GetBytes(content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Close(); } else { response.Write(content); } } } else { content = "/* Not cached */" + content; gzipStream = new GZipOutputStream(response.OutputStream); buff = encoding.GetBytes(content.ToCharArray()); gzipStream.Write(buff, 0, buff.Length); gzipStream.Close(); } } else { content = "/* Not cached */" + content; response.Write(content); } }