예제 #1
1
 /// <summary>
 /// 
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public static byte[] Compress(byte[] data)
 {
     using (var compressedStream = new MemoryStream())
     using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
     {
         zipStream.Write(data, 0, data.Length);
         zipStream.Close();
         return compressedStream.ToArray();
     }
 }
예제 #2
0
    public static MemoryStream readStream(FileStream stream)
    {
        MemoryStream outStream = new MemoryStream();

        GZipStream compress = new GZipStream(stream, CompressionMode.Decompress, false);

        byte[] buffer = new Byte[stream.Length];

        while (true)
        {
            int count = compress.Read(buffer, 0, buffer.Length);

            if (count != 0)
            {
                outStream.Write(buffer, 0, buffer.Length);
            }

            if (count != buffer.Length)
            {
                break;
            }
        }

        compress.Close();
        outStream.Close();
        stream.Close();

        return new MemoryStream(outStream.ToArray());
    }
예제 #3
0
        private void DownloadBtn_Click(object sender, EventArgs e)
        {
            FileStream   sourceStream       = null;
            GZipStream   uncompressedStream = null;
            StreamReader textReader         = null;

            try
            {
                sourceStream = new FileStream("GZipFileExample.gz", System.IO.FileMode.Open,
                                              FileAccess.Read, FileShare.Read);

                uncompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);

                textReader        = new StreamReader(uncompressedStream, true);
                richTextBox1.Text = textReader.ReadToEnd();
            }
            catch (FileNotFoundException exception)
            {
                throw new LoadFileException("Произошла ошибка при загрузке файла:\n", exception);
            }
            catch (UnauthorizedAccessException exception)
            {
                throw new LoadFileException("Произошла ошибка при загрузке файла:\n", exception);
            }
            finally
            {
                sourceStream?.Close();
                uncompressedStream?.Close();
                textReader?.Close();
            }
        }
예제 #4
0
        private static void CompressUnsafe(ref Stream outputStream, out Stream gzippedStream)
        {
            byte[] contentBuffer = new byte[102400];

            GZipStream gzipCompression = null;

            try
            {
                gzippedStream   = new MemoryStream();
                gzipCompression = new GZipStream(gzippedStream, CompressionMode.Compress, true);

                do
                {
                    int bC = outputStream.Read(contentBuffer, 0, contentBuffer.Length);
                    if (bC == 0)
                    {
                        break;
                    }

                    gzipCompression.Write(contentBuffer, 0, bC);
                } while (true);
            }
            finally
            {
                gzipCompression?.Close();
            }
        }
예제 #5
0
    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            usage();
            return;
        }
        else
        {
            string inputFile = args[0];
            string outputFile = inputFile + ".gz";

            try
            {
                // Get bytes from input stream
                FileStream inFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, inputFile), FileMode.Open);
                byte[] buffer = new byte[inFileStream.Length];
                inFileStream.Read(buffer, 0, buffer.Length);
                inFileStream.Close();

                // Create GZip file stream and compress input bytes
                FileStream outFileStream = new FileStream(Path.Combine(Environment.CurrentDirectory, outputFile), FileMode.Create);
                GZipStream compressedStream = new GZipStream(outFileStream, CompressionMode.Compress);
                compressedStream.Write(buffer, 0, buffer.Length);
                compressedStream.Close();
                outFileStream.Close();

                Console.WriteLine("The file has been compressed.  UR Da Bomb!!!");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Error: Specified file cannot be found.");
            }
        }
    }
 public static byte[] Compress(byte[] data)
 {
     MemoryStream output = new MemoryStream();
     GZipStream gzip = new GZipStream(output,
                       CompressionMode.Compress, true);
     gzip.Write(data, 0, data.Length);
     gzip.Close();
     return output.ToArray();
 } 
        public void TestInit()
        {
            Stream      baseStream = new MemoryStream();
            GZipStream  gzipStream = new GZipStream(baseStream, CompressionMode.Decompress);

            Assert.AreSame(baseStream, gzipStream.BaseStream);

            gzipStream.Close();
        }
예제 #8
0
    public static byte[] Compress(this byte[] bytes)
    {
        using (MemoryStream ms = new MemoryStream()) {
            GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
            Compress.Write(bytes, 0, bytes.Length);
            Compress.Close();

            return ms.ToArray();
        }
    }
예제 #9
0
    public static byte[] Decompress(this Byte[] bytes)
    {
        using (MemoryStream tempMs = new MemoryStream()) {
            using (MemoryStream ms = new MemoryStream(bytes)) {
                GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);
                Decompress.CopyTo(tempMs);
                Decompress.Close();

                return tempMs.ToArray();
            }
        }
    }
예제 #10
0
 /// <summary>
 ///     A string extension method that compress the given string to GZip byte array.
 /// </summary>
 /// <param name="stringToCompress">The stringToCompress to act on.</param>
 /// <returns>The string compressed into a GZip byte array.</returns>
 /// <example>
 ///     <code>
 ///           using System;
 ///           using Microsoft.VisualStudio.TestTools.UnitTesting;
 ///           using Z.ExtensionMethods;
 ///           
 ///           namespace ExtensionMethods.Examples
 ///           {
 ///               [TestClass]
 ///               public class System_String_CompressGZip
 ///               {
 ///                   [TestMethod]
 ///                   public void CompressGZip()
 ///                   {
 ///                       // Type
 ///                       string @this = &quot;FizzBuzz&quot;;
 ///           
 ///                       // Exemples
 ///                       var result = @this.CompressGZip();
 ///           
 ///                       // Unit Test
 ///                       Assert.AreEqual(&quot;FizzBuzz&quot;, result.DecompressGZip());
 ///                   }
 ///               }
 ///           }
 ///     </code>
 /// </example>
 public static byte[] CompressGZip(this string stringToCompress)
 {
     byte[] stringAsBytes = Encoding.Default.GetBytes(stringToCompress);
     using (var memoryStream = new MemoryStream())
     {
         using (var zipStream = new GZipStream(memoryStream, CompressionMode.Compress))
         {
             zipStream.Write(stringAsBytes, 0, stringAsBytes.Length);
             zipStream.Close();
             return (memoryStream.ToArray());
         }
     }
 }
        public void TestDecompress()
        {
            Stream      baseStream = new MemoryStream(this.compressedData);
            GZipStream  gzipStream = new GZipStream(baseStream, CompressionMode.Decompress);

            StreamReader reader = new StreamReader(gzipStream);

            string  data = reader.ReadToEnd();

            Assert.AreEqual(Data, data);

            gzipStream.Close();
        }
예제 #12
0
 /// <summary>
 /// 将指定的文件解压,返回解压后的数据
 /// </summary>
 /// <param name="srcFile">指定的源文件</param>
 /// <returns>解压后得到的数据</returns>
 public static byte[] DecompressData(string srcFile)
 {
     if (File.Exists(srcFile))
     {
         FileStream sourceStream       = null;
         GZipStream decompressedStream = null;
         byte[]     quartetBuffer2     = null;
         try
         {
             sourceStream       = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
             decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
             quartetBuffer2     = new byte[4];
             long position = sourceStream.Position = sourceStream.Length - 4;
             sourceStream.Read(quartetBuffer2, 0, 4);
             int    checkLength = BitConverter.ToInt32(quartetBuffer2, 0);
             byte[] data        = (checkLength > sourceStream.Length) ? new byte[checkLength + 100] : new byte[32767];
             byte[] buffer      = new byte[100];
             sourceStream.Position = 0L;
             int offset = 0;
             int total  = 0;
             while (true)
             {
                 int bytesRead = decompressedStream.Read(buffer, 0, 100);
                 if (bytesRead == 0)
                 {
                     break;
                 }
                 buffer.CopyTo(data, offset);
                 offset += bytesRead;
                 total  += bytesRead;
             }
             byte[] actualdata = new byte[total];
             for (int i = 0; i < total; i++)
             {
                 actualdata[i] = data[i];
             }
             return(actualdata);
         }
         catch (Exception ex)
         {
             throw new Exception($"从文件{srcFile}解压数据时发生错误", ex);
         }
         finally
         {
             sourceStream?.Close();
             decompressedStream?.Close();
         }
     }
     throw new FileNotFoundException($"找不到指定的文件{srcFile}");
 }
예제 #13
0
        /// <summary>
        /// Read the log file until a cancellation is requested.
        /// </summary>
        private void Run()
        {
            try
            {
                var timer = Stopwatch.StartNew();
                int count = 0;
                while (!cancellationToken.IsCancellationRequested)
                {
                    // not going to use async read here since I only have 1 instance of the background task
                    // and it's okay if it blocked
                    var line = reader.ReadLine();

                    // report progress
                    if (line == null || count % 500 == 0)
                    {
                        percent = (float)stream.Position / stream.Length;
                        elapsed = timer.Elapsed.TotalSeconds;
                        progress?.Report(new LogReaderStatus()
                        {
                            Percent = percent,
                            Line    = count,
                            Notes   = elapsed.ToString("F1") + "s"
                        });
                    }

                    // if the end of the stream is reached then sleep for a while
                    if (line == null)
                    {
                        timer.Stop();
                        Thread.Sleep(100);
                        continue;
                    }

                    // process line
                    lastLine = line;
                    handler(line);
                    count += 1;
                }
            }
            finally
            {
                reader?.Close();
                gzip?.Close();
                stream?.Close();
            }
        }
예제 #14
0
    public static void saveStream(Stream stream, String location)
    {
        FileStream outStream = File.Create(location);
        GZipStream compress = new GZipStream(outStream, CompressionMode.Compress, false);

        byte[] buffer = new Byte[stream.Length];

        int read = stream.Read(buffer, 0, buffer.Length);

        while (read > 0)
        {
            compress.Write(buffer, 0, read);
            read = stream.Read(buffer, 0, buffer.Length);
        }

        compress.Close();
        outStream.Close();
        stream.Close();
    }
 public static byte[] Decompress(byte[] data)
 {
     MemoryStream input = new MemoryStream();
     input.Write(data, 0, data.Length);
     input.Position = 0;
     GZipStream gzip = new GZipStream(input,
                       CompressionMode.Decompress, true);
     MemoryStream output = new MemoryStream();
     byte[] buff = new byte[64];
     int read = -1;
     read = gzip.Read(buff, 0, buff.Length);
     while (read > 0)
     {
         output.Write(buff, 0, read);
         read = gzip.Read(buff, 0, buff.Length);
     }
     gzip.Close();
     return output.ToArray();
 }
예제 #16
0
파일: RarHelper.cs 프로젝트: lakeli/shizong
    public static void CompressFile(string toCompressFileName,string targetFileName,bool IsDeleteSourceFile)
    {
        FileStream reader;
        reader = File.Open(toCompressFileName, FileMode.Open);
        FileStream writer;
        writer = File.Create(targetFileName);
       
        //压缩相关的流
        MemoryStream ms = new MemoryStream();
        GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress, true);


        //往压缩流中写数据
        byte[] sourceBuffer = new byte[reader.Length];
        reader.Read(sourceBuffer, 0, sourceBuffer.Length);
        zipStream.Write(sourceBuffer, 0, sourceBuffer.Length);
       
        //一定要在内存流读取之前关闭压缩流
        zipStream.Close();
        zipStream.Dispose();
       
        //从内存流中读数据
        ms.Position = 0; //注意,不要遗漏此句
        byte[] destBuffer = new byte[ms.Length];
        ms.Read(destBuffer, 0, destBuffer.Length);
        writer.Write(destBuffer, 0, destBuffer.Length);
       
        //关闭并释放内存流
        ms.Close();
        ms.Dispose();
       
        //关闭并释放文件流
        writer.Close();
        writer.Dispose();
        reader.Close();
        reader.Dispose();
        if (IsDeleteSourceFile)
        {
            File.Delete(toCompressFileName);
        }
    }
예제 #17
0
        private async Task DowloadAndReadDumpsAsync()
        {
            GZipStream regionsStream = null;
            GZipStream nationsStream = null;

            try
            {
                Stopwatch stopWatch = Stopwatch.StartNew();
                regionsStream = await _apiService.GetNationStatesDumpStream(NationStatesDumpType.Regions);

                stopWatch.Stop();
                _logger.LogDebug(defaultEventId, GetLogMessage($"Download region dump as stream took {stopWatch.Elapsed} to complete."));
                stopWatch.Restart();
                nationsStream = await _apiService.GetNationStatesDumpStream(NationStatesDumpType.Nations);

                stopWatch.Stop();
                _logger.LogDebug(defaultEventId, GetLogMessage($"Download nation dump as stream took {stopWatch.Elapsed} to complete."));
                stopWatch.Restart();
                await WriteDumpToLocalFileSystemAsync(NationStatesDumpType.Regions, regionsStream);

                stopWatch.Stop();
                _logger.LogDebug(defaultEventId, GetLogMessage($"Writing region dump to local cache took {stopWatch.Elapsed} to complete."));
                stopWatch.Restart();
                await WriteDumpToLocalFileSystemAsync(NationStatesDumpType.Nations, nationsStream);

                stopWatch.Stop();
                _logger.LogDebug(defaultEventId, GetLogMessage($"Writing nation dump from local cache took {stopWatch.Elapsed} to complete."));
                ReadDumpsFromLocalFileSystem();
            }
            finally
            {
                nationsStream?.Close();
                regionsStream?.Close();
                nationsStream?.Dispose();
                regionsStream?.Dispose();
            }
        }
예제 #18
0
        private void ReadDumpsFromLocalFileSystem()
        {
            GZipStream regionStream = null;
            GZipStream nationStream = null;
            FileStream fsr          = null;
            FileStream fsn          = null;

            try
            {
                Stopwatch stopWatch = Stopwatch.StartNew();
                fsr          = new FileStream(regionFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                regionStream = new GZipStream(fsr, CompressionMode.Decompress);

                stopWatch.Stop();
                _logger.LogDebug(defaultEventId, GetLogMessage($"Reading region dump from local cache took {stopWatch.Elapsed} to complete."));
                stopWatch.Restart();
                fsn          = new FileStream(nationFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                nationStream = new GZipStream(fsn, CompressionMode.Decompress);

                stopWatch.Stop();
                _logger.LogDebug(defaultEventId, GetLogMessage($"Reading nation dump from local cache took {stopWatch.Elapsed} to complete."));
                var fileInfoNations = new FileInfo(nationFileName);
                LastDumpUpdateTimeUtc = fileInfoNations.LastWriteTimeUtc;
                LoadDumpsFromStream(regionStream, nationStream);
            }
            finally
            {
                fsr?.Close();
                fsn?.Close();
                nationStream?.Close();
                regionStream?.Close();
                fsr.Close();
                fsn.Close();
                nationStream?.Dispose();
                regionStream?.Dispose();
            }
        }
예제 #19
0
파일: Compress.cs 프로젝트: QYFS2018/VC2
    public void CompressFile(string sourceFile, string destinationFile)
    {
        try
        {
            sourceFile      = HttpContext.Current.Request.PhysicalApplicationPath + sourceFile;
            destinationFile = HttpContext.Current.Request.PhysicalApplicationPath + destinationFile;
        }
        catch { }

        // make sure the source file is there
        if (File.Exists(sourceFile) == false)
        {
            throw new FileNotFoundException();
        }

        // Create the streams and byte arrays needed
        byte[]     buffer            = null;
        FileStream sourceStream      = null;
        FileStream destinationStream = null;
        GZipStream compressedStream  = null;

        try
        {
            // Read the bytes from the source file into a byte array
            sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);

            // Read the source stream values into the buffer
            buffer = new byte[sourceStream.Length];
            int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);

            if (checkCounter != buffer.Length)
            {
                throw new ApplicationException();
            }

            // Open the FileStream to write to
            destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);

            // Create a compression stream pointing to the destiantion stream
            compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);

            // Now write the compressed data to the destination file
            compressedStream.Write(buffer, 0, buffer.Length);
        }
        catch (ApplicationException ex)
        {
            throw new ApplicationException(ex.ToString());
        }
        finally
        {
            // Make sure we allways close all streams
            if (sourceStream != null)
            {
                sourceStream.Close();
            }

            if (compressedStream != null)
            {
                compressedStream.Close();
            }

            if (destinationStream != null)
            {
                destinationStream.Close();
            }
        }
    }
예제 #20
0
    } // End of the GetSqlString method

    #endregion

    #region Database versioning

    /// <summary>
    /// Save the database version to an xml file
    /// </summary>
    /// <param name="databaseVersion">The database version to update to</param>
    /// <returns>A error message</returns>
    public static void SetDatabaseVersion(Int32 databaseVersion)
    {

        // Set the filename
        string filename = HttpContext.Current.Server.MapPath("/DatabaseFiles/DatabaseVersion.xml.gz");

        // Create variables
        GZipStream gzipStream = null;
        XmlTextWriter xmlTextWriter = null;

        try
        {
            // Create a gzip stream
            gzipStream = new GZipStream(new FileStream(filename, FileMode.Create), CompressionMode.Compress);

            // Create a xml text writer
            xmlTextWriter = new XmlTextWriter(gzipStream, new UTF8Encoding(true));

            // Write the start tag of the document
            xmlTextWriter.WriteStartDocument();

            // Write the root element
            xmlTextWriter.WriteStartElement("Database");

            // Write information to the document
            CreateXmlRow(xmlTextWriter, "Version", databaseVersion.ToString());

            // Write the end tag for the xml document
            xmlTextWriter.WriteEndElement();
            xmlTextWriter.WriteEndDocument();

        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            // Close streams
            if (xmlTextWriter != null)
            {
                // Close the XmlTextWriter
                xmlTextWriter.Close();
            }
            if (gzipStream != null)
            {
                // Close the gzip stream
                gzipStream.Close();
            }
        }

    } // End of the SetDatabaseVersion method
예제 #21
0
        public void Patch(string oldFile, string newFile, string patchfile, Compression compression = Compression.BZip2)
        {
            long controlLength, dataLength, newSize;

            /*
             * File format:
             * 0	8	"BSDIFF40"
             * 8	8	X
             * 16	8	Y
             * 24	8	sizeof(newfile)
             * 32	X	bzip2(control block)
             * 32+X	Y	bzip2(diff block)
             * 32+X+Y	???	bzip2(extra block)
             * with control block a set of triples (x,y,z) meaning "add x bytes
             * from oldfile to x bytes from the diff block; copy y bytes from the
             * extra block; seek forwards in oldfile by z bytes".
             */

            using (FileStream header = File.OpenRead(patchfile))
            {
                byte[] headerBuf = new byte[32];

                if (header.Read(headerBuf, 0, 32) < 32)
                {
                    throw new Exception("invalid patch file (too small)");
                }

                if (!buffcmp(headerBuf, "BSDIFF40", 0, 8))
                {
                    throw new Exception("invalid patch file (no magic)");
                }

                controlLength = offtin(headerBuf, 8);
                dataLength    = offtin(headerBuf, 16);
                newSize       = offtin(headerBuf, 24);

                if (controlLength < 0 || dataLength < 0 || newSize < 0)
                {
                    throw new Exception("invalid patch file (sizes are corrupt)");
                }
            }

            long progressSize = newSize * 3;

            Stream controlStream, diffStream, extraStream;

            byte[] cfs, dfs, efs;

            using (FileStream stream = File.OpenRead(patchfile))
            {
                stream.Seek(32, SeekOrigin.Begin);

                cfs = new byte[controlLength];
                stream.Read(cfs, 0, cfs.Length);

                dfs = new byte[dataLength];
                stream.Read(dfs, 0, dfs.Length);

                efs = new byte[stream.Length - stream.Position];
                stream.Read(efs, 0, efs.Length);
            }

            switch (compression)
            {
            case Compression.BZip2:
                controlStream = new BZip2InputStream(new MemoryStream(cfs));
                diffStream    = new BZip2InputStream(new MemoryStream(dfs));
                extraStream   = new BZip2InputStream(new MemoryStream(efs));
                break;

            default:
                controlStream = new GZipStream(new MemoryStream(cfs), CompressionMode.Decompress, CompressionLevel.BestCompression);
                diffStream    = new GZipStream(new MemoryStream(dfs), CompressionMode.Decompress, CompressionLevel.BestCompression);
                extraStream   = new GZipStream(new MemoryStream(efs), CompressionMode.Decompress, CompressionLevel.BestCompression);
                break;
            }

            byte[] oldFileBytes;

            int segment_length = 1048573;

            using (FileStream stream = new FileStream(oldFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                long length = stream.Length;
                oldFileBytes = new byte[(int)length];
                for (int i = 0; i < oldFileBytes.Length; i += segment_length)
                {
                    stream.Read(oldFileBytes, i, Math.Min(segment_length, oldFileBytes.Length - i));
                    updateProgress(i, progressSize);
                }
            }

            byte[] newFileBytes = new byte[newSize];

            long oldSize = oldFileBytes.Length;

            int newPos = 0;
            int oldPos = 0;

            int[]  ctrl = new int[3];
            byte[] buf  = new byte[8];

            /* Read control data */
            while (newPos < newSize)
            {
                int lenRead;

                for (int i = 0; i <= 2; i++)
                {
                    lenRead = controlStream.Read(buf, 0, 8);
                    if (lenRead < 8)
                    {
                        throw new Exception("invalid patch file (corrupt)");
                    }
                    ctrl[i] = offtin(buf, 0);
                }

                if (newPos + ctrl[0] > newSize)
                {
                    throw new Exception("invalid patch file (corrupt)");
                }

                /* Read diff string */
                lenRead = 0;

                for (int i = newPos; i < newPos + ctrl[0]; i += 65536)
                {
                    lenRead += diffStream.Read(newFileBytes, i, Math.Min(65536, (newPos + ctrl[0]) - i));
                    updateProgress(newSize + newPos + lenRead, progressSize);
                }

                if (lenRead < ctrl[0])
                {
                    throw new Exception("invalid patch file (corrupt)");
                }

                /* Add old data to diff string */
                for (int i = 0; i < ctrl[0]; i++)
                {
                    if ((oldPos + i >= 0) && (oldPos + i < oldSize))
                    {
                        newFileBytes[newPos + i] += oldFileBytes[oldPos + i];
                    }
                }

                /* Adjust pointers */
                newPos += ctrl[0];
                oldPos += ctrl[0];

                if (newPos > newSize)
                {
                    throw new Exception("invalid patch file (corrupt)");
                }

                /* Read extra string */
                lenRead = extraStream.Read(newFileBytes, newPos, ctrl[1]);
                if (lenRead < ctrl[1])
                {
                    throw new Exception("invalid patch file (corrupt)");
                }

                /* Sanity-check */
                newPos += ctrl[1];
                oldPos += ctrl[2];
            }

            controlStream.Close();
            diffStream.Close();
            extraStream.Close();

            using (FileStream str = File.Create(newFile))
            {
                for (int i = 0; i < newFileBytes.Length; i += segment_length)
                {
                    str.Write(newFileBytes, i, Math.Min(segment_length, newFileBytes.Length - i));
                    updateProgress(newSize * 2 + i, progressSize);
                }
            }

            if (OnProgress != null)
            {
                OnProgress(this, 1, 1);
            }
        }
예제 #22
0
        public void Zlib_DisposedException_GZipStream()
        {
            string TextToCompress =  IhaveaDream;

            MemoryStream ms1= new MemoryStream();

            Stream compressor= new GZipStream(ms1, CompressionMode.Compress, false);

            TestContext.WriteLine("Text to compress is {0} bytes: '{1}'",
                                  TextToCompress.Length, TextToCompress);
            TestContext.WriteLine("using compressor: {0}", compressor.GetType().FullName);

            StreamWriter sw = new StreamWriter(compressor, Encoding.ASCII);
            sw.Write(TextToCompress);
            sw.Close(); // implicitly closes compressor
            sw.Close(); // implicitly closes compressor, again

            compressor.Close(); // explicitly closes compressor
            var a = ms1.ToArray();
            TestContext.WriteLine("Compressed stream is {0} bytes long", a.Length);

            var ms2 = new MemoryStream(a);
            Stream decompressor  = new GZipStream(ms2, CompressionMode.Decompress, false);

            TestContext.WriteLine("using decompressor: {0}", decompressor.GetType().FullName);

            var sr = new StreamReader(decompressor, Encoding.ASCII);
            string DecompressedText = sr.ReadToEnd();
            sr.Close();

            TestContext.WriteLine("decompressor.CanRead = {0}",decompressor.CanRead);

            TestContext.WriteLine("Read {0} characters: '{1}'", DecompressedText.Length, DecompressedText);
            TestContext.WriteLine("\n");
            Assert.AreEqual<String>(TextToCompress, DecompressedText);
        }
예제 #23
0
파일: MemUtil.cs 프로젝트: elitak/keepass
        public static byte[] Decompress(byte[] pbCompressed)
        {
            if(pbCompressed == null) throw new ArgumentNullException("pbCompressed");
            if(pbCompressed.Length == 0) return pbCompressed;

            MemoryStream msCompressed = new MemoryStream(pbCompressed, false);
            GZipStream gz = new GZipStream(msCompressed, CompressionMode.Decompress);
            MemoryStream msData = new MemoryStream();
            MemUtil.CopyStream(gz, msData);
            gz.Close();
            msCompressed.Close();

            byte[] pbData = msData.ToArray();
            msData.Close();
            return pbData;
        }
예제 #24
0
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="sourceFile">源文件</param>
        /// <param name="destinationFile">目标文件</param>
        public static void DeCompressFile(string sourceFile, string destinationFile)
        {
            if (File.Exists(sourceFile) == false)
            {
                throw new FileNotFoundException();
            }

            FileStream sourceStream       = null;
            FileStream destinationStream  = null;
            GZipStream decompressedStream = null;

            byte[] quartetBuffer = null;

            try
            {
                sourceStream = new FileStream(sourceFile, FileMode.Open);

                decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);

                quartetBuffer = new byte[4];
                int position = (int)sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);
                sourceStream.Position = 0;
                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);

                byte[] buffer = new byte[checkLength + 100];

                int offset = 0;
                int total  = 0;

                while (true)
                {
                    int bytesRead = decompressedStream.Read(buffer, offset, 100);

                    if (bytesRead == 0)
                    {
                        break;
                    }

                    offset += bytesRead;
                    total  += bytesRead;
                }

                destinationStream = new FileStream(destinationFile, FileMode.Create);
                destinationStream.Write(buffer, 0, total);

                destinationStream.Flush();
            }
            catch (ApplicationException ex)
            {
                throw (ex);
            }
            finally
            {
                if (sourceStream != null)
                {
                    sourceStream.Close();
                }

                if (decompressedStream != null)
                {
                    decompressedStream.Close();
                }

                if (destinationStream != null)
                {
                    destinationStream.Close();
                }
            }
        }
예제 #25
0
        public RegionData LoadBackup(string file)
        {
            if (!File.Exists(file))
            {
                return(null);
            }

            var stream = ArchiveHelpers.GetStream(file);

            if (stream == null)
            {
                return(null);
            }

            GZipStream       m_loadStream  = new GZipStream(stream, CompressionMode.Decompress);
            TarArchiveReader reader        = new TarArchiveReader(m_loadStream);
            List <uint>      foundLocalIDs = new List <uint>();
            RegionData       regiondata    = new RegionData();

            regiondata.Init();

            byte[] data;
            string filePath;

            TarArchiveReader.TarEntryType entryType;
            System.Collections.Concurrent.ConcurrentQueue <byte[]> groups =
                new System.Collections.Concurrent.ConcurrentQueue <byte[]>();
            //Load the archive data that we need
            while ((data = reader.ReadEntry(out filePath, out entryType)) != null)
            {
                if (TarArchiveReader.TarEntryType.TYPE_DIRECTORY == entryType)
                {
                    continue;
                }

                if (filePath.StartsWith("parcels/"))
                {
                    //Only use if we are not merging
                    LandData parcel     = new LandData();
                    OSD      parcelData = OSDParser.DeserializeLLSDBinary(data);
                    parcel.FromOSD((OSDMap)parcelData);
                    if (parcel.OwnerID != UUID.Parse("05948863-b678-433e-87a4-e44d17678d1d"))
                    {
                        //The default owner of the 'default' region
                        regiondata.Parcels.Add(parcel);
                    }
                }
                else if (filePath.StartsWith("newstyleterrain/"))
                {
                    regiondata.Terrain = data;
                }
                else if (filePath.StartsWith("newstylerevertterrain/"))
                {
                    regiondata.RevertTerrain = data;
                }
                else if (filePath.StartsWith("newstylewater/"))
                {
                    regiondata.Water = data;
                }
                else if (filePath.StartsWith("newstylerevertwater/"))
                {
                    regiondata.RevertWater = data;
                }
                else if (filePath.StartsWith("entities/"))
                {
                    groups.Enqueue(data);
                }
                else if (filePath.StartsWith("regioninfo/"))
                {
                    RegionInfo info = new RegionInfo();
                    info.FromOSD((OSDMap)OSDParser.DeserializeLLSDBinary(data));
                    regiondata.RegionInfo = info;
                }
                data = null;
            }
            m_loadStream.Close();
            m_loadStream = null;

            int threadCount = groups.Count > 16 ? 16 : groups.Count;

            System.Threading.Thread[] threads = new System.Threading.Thread[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                threads[i] = new System.Threading.Thread(() =>
                {
                    byte[] groupData;
                    while (groups.TryDequeue(out groupData))
                    {
                        MemoryStream ms          = new MemoryStream(groupData);
                        ISceneEntity sceneObject =
                            SceneEntitySerializer.SceneObjectSerializer
                            .FromXml2Format(ref ms,
                                            null);
                        ms.Close();
                        ms   = null;
                        data = null;
                        if (sceneObject != null)
                        {
                            foreach (
                                ISceneChildEntity part in
                                sceneObject.ChildrenEntities())
                            {
                                lock (foundLocalIDs)
                                {
                                    if (
                                        !foundLocalIDs.Contains(
                                            part.LocalId))
                                    {
                                        foundLocalIDs.Add(part.LocalId);
                                    }
                                    else
                                    {
                                        part.LocalId = 0;
                                    }
                                    //Reset it! Only use it once!
                                }
                            }
                            regiondata.Groups.Add(
                                sceneObject as SceneObjectGroup);
                        }
                    }
                });
                threads[i].Start();
            }
            for (int i = 0; i < threadCount; i++)
            {
                threads[i].Join();
            }

            foundLocalIDs.Clear();

            return(regiondata);
        }
예제 #26
0
    static void GenerateBundles(List <string> files)
    {
        string temp_s = "temp.s";         // Path.GetTempFileName ();
        string temp_c = "temp.c";
        string temp_o = "temp.o";

        if (compile_only)
        {
            temp_c = output;
        }
        if (object_out != null)
        {
            temp_o = object_out;
        }

        try {
            List <string>   c_bundle_names = new List <string> ();
            List <string[]> config_names   = new List <string[]> ();

            using (StreamWriter ts = new StreamWriter(File.Create(temp_s))) {
                using (StreamWriter tc = new StreamWriter(File.Create(temp_c))) {
                    string prog = null;

#if XAMARIN_ANDROID
                    tc.WriteLine("/* This source code was produced by mkbundle, do not edit */");
                    tc.WriteLine("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
                    tc.WriteLine(@"
typedef struct {
	const char *name;
	const unsigned char *data;
	const unsigned int size;
} MonoBundledAssembly;
void          mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
void          mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
");
#else
                    tc.WriteLine("#include <mono/metadata/mono-config.h>");
                    tc.WriteLine("#include <mono/metadata/assembly.h>\n");
#endif

                    if (compress)
                    {
                        tc.WriteLine("typedef struct _compressed_data {");
                        tc.WriteLine("\tMonoBundledAssembly assembly;");
                        tc.WriteLine("\tint compressed_size;");
                        tc.WriteLine("} CompressedAssembly;\n");
                    }

                    object monitor = new object();

                    var streams = new Dictionary <string, Stream> ();
                    var sizes   = new Dictionary <string, long> ();

                    // Do the file reading and compression in parallel
                    Action <string> body = delegate(string url) {
                        string fname  = LocateFile(new Uri(url).LocalPath);
                        Stream stream = File.OpenRead(fname);

                        long real_size = stream.Length;
                        int  n;
                        if (compress)
                        {
                            byte[]       cbuffer = new byte [8192];
                            MemoryStream ms      = new MemoryStream();
                            GZipStream   deflate = new GZipStream(ms, CompressionMode.Compress, leaveOpen: true);
                            while ((n = stream.Read(cbuffer, 0, cbuffer.Length)) != 0)
                            {
                                deflate.Write(cbuffer, 0, n);
                            }
                            stream.Close();
                            deflate.Close();
                            byte [] bytes = ms.GetBuffer();
                            stream = new MemoryStream(bytes, 0, (int)ms.Length, false, false);
                        }

                        lock (monitor) {
                            streams [url] = stream;
                            sizes [url]   = real_size;
                        }
                    };

                    //#if NET_4_5
#if FALSE
                    Parallel.ForEach(files, body);
#else
                    foreach (var url in files)
                    {
                        body(url);
                    }
#endif

                    // The non-parallel part
                    byte [] buffer = new byte [8192];
                    // everything other than a-zA-Z0-9_ needs to be escaped in asm symbols.
                    var symbolEscapeRE = new System.Text.RegularExpressions.Regex("[^\\w_]");
                    foreach (var url in files)
                    {
                        string fname   = LocateFile(new Uri(url).LocalPath);
                        string aname   = Path.GetFileName(fname);
                        string encoded = symbolEscapeRE.Replace(aname, "_");

                        if (prog == null)
                        {
                            prog = aname;
                        }

                        var stream    = streams [url];
                        var real_size = sizes [url];

                        if (!quiet)
                        {
                            Console.WriteLine("   embedding: " + fname);
                        }

                        WriteSymbol(ts, "assembly_data_" + encoded, stream.Length);

                        WriteBuffer(ts, stream, buffer);

                        if (compress)
                        {
                            tc.WriteLine("extern const unsigned char assembly_data_{0} [];", encoded);
                            tc.WriteLine("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
                                         " assembly_data_{0}, {2}}}, {3}}};",
                                         encoded, aname, real_size, stream.Length);
                            if (!quiet)
                            {
                                double ratio = ((double)stream.Length * 100) / real_size;
                                Console.WriteLine("   compression ratio: {0:.00}%", ratio);
                            }
                        }
                        else
                        {
                            tc.WriteLine("extern const unsigned char assembly_data_{0} [];", encoded);
                            tc.WriteLine("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
                                         encoded, aname, real_size);
                        }
                        stream.Close();

                        c_bundle_names.Add("assembly_bundle_" + encoded);

                        try {
                            FileStream cf = File.OpenRead(fname + ".config");
                            if (!quiet)
                            {
                                Console.WriteLine(" config from: " + fname + ".config");
                            }
                            tc.WriteLine("extern const unsigned char assembly_config_{0} [];", encoded);
                            WriteSymbol(ts, "assembly_config_" + encoded, cf.Length);
                            WriteBuffer(ts, cf, buffer);
                            ts.WriteLine();
                            config_names.Add(new string[] { aname, encoded });
                        } catch (FileNotFoundException) {
                            /* we ignore if the config file doesn't exist */
                        }
                    }

                    if (config_file != null)
                    {
                        FileStream conf;
                        try {
                            conf = File.OpenRead(config_file);
                        } catch {
                            Error(String.Format("Failure to open {0}", config_file));
                            return;
                        }
                        if (!quiet)
                        {
                            Console.WriteLine("System config from: " + config_file);
                        }
                        tc.WriteLine("extern const char system_config;");
                        WriteSymbol(ts, "system_config", config_file.Length);

                        WriteBuffer(ts, conf, buffer);
                        // null terminator
                        ts.Write("\t.byte 0\n");
                        ts.WriteLine();
                    }

                    if (machine_config_file != null)
                    {
                        FileStream conf;
                        try {
                            conf = File.OpenRead(machine_config_file);
                        } catch {
                            Error(String.Format("Failure to open {0}", machine_config_file));
                            return;
                        }
                        if (!quiet)
                        {
                            Console.WriteLine("Machine config from: " + machine_config_file);
                        }
                        tc.WriteLine("extern const char machine_config;");
                        WriteSymbol(ts, "machine_config", machine_config_file.Length);

                        WriteBuffer(ts, conf, buffer);
                        ts.Write("\t.byte 0\n");
                        ts.WriteLine();
                    }
                    ts.Close();

                    if (compress)
                    {
                        tc.WriteLine("\nstatic const CompressedAssembly *compressed [] = {");
                    }
                    else
                    {
                        tc.WriteLine("\nstatic const MonoBundledAssembly *bundled [] = {");
                    }

                    foreach (string c in c_bundle_names)
                    {
                        tc.WriteLine("\t&{0},", c);
                    }
                    tc.WriteLine("\tNULL\n};\n");
                    tc.WriteLine("static char *image_name = \"{0}\";", prog);

                    if (ctor_func != null)
                    {
                        tc.WriteLine("\nextern void {0} (void);", ctor_func);
                        tc.WriteLine("\n__attribute__ ((constructor)) static void mono_mkbundle_ctor (void)");
                        tc.WriteLine("{{\n\t{0} ();\n}}", ctor_func);
                    }

                    tc.WriteLine("\nstatic void install_dll_config_files (void) {\n");
                    foreach (string[] ass in config_names)
                    {
                        tc.WriteLine("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
                    }
                    if (config_file != null)
                    {
                        tc.WriteLine("\tmono_config_parse_memory (&system_config);\n");
                    }
                    if (machine_config_file != null)
                    {
                        tc.WriteLine("\tmono_register_machine_config (&machine_config);\n");
                    }
                    tc.WriteLine("}\n");

                    if (config_dir != null)
                    {
                        tc.WriteLine("static const char *config_dir = \"{0}\";", config_dir);
                    }
                    else
                    {
                        tc.WriteLine("static const char *config_dir = NULL;");
                    }

                    Stream template_stream;
                    if (compress)
                    {
                        template_stream = System.Reflection.Assembly.GetAssembly(typeof(MakeBundle)).GetManifestResourceStream("template_z.c");
                    }
                    else
                    {
                        template_stream = System.Reflection.Assembly.GetAssembly(typeof(MakeBundle)).GetManifestResourceStream("template.c");
                    }

                    StreamReader s        = new StreamReader(template_stream);
                    string       template = s.ReadToEnd();
                    tc.Write(template);

                    if (!nomain && custom_main == null)
                    {
                        Stream       template_main_stream = System.Reflection.Assembly.GetAssembly(typeof(MakeBundle)).GetManifestResourceStream("template_main.c");
                        StreamReader st           = new StreamReader(template_main_stream);
                        string       maintemplate = st.ReadToEnd();
                        tc.Write(maintemplate);
                    }

                    tc.Close();

                    string assembler = GetEnv("AS", "as");
                    string as_cmd    = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
                    Execute(as_cmd);

                    if (compile_only)
                    {
                        return;
                    }

                    if (!quiet)
                    {
                        Console.WriteLine("Compiling:");
                    }

                    if (style == "windows")
                    {
                        Func <string, string> quote = (pp) => { return("\"" + pp + "\""); };

                        string compiler   = GetEnv("CC", "cl.exe");
                        string winsdkPath = GetEnv("WINSDK", @"C:\Program Files (x86)\Windows Kits\8.1");
                        string vsPath     = GetEnv("VSINCLUDE", @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC");
                        string monoPath   = GetEnv("MONOPREFIX", @"C:\Program Files (x86)\Mono");

                        string[] includes      = new string[] { winsdkPath + @"\Include\um", winsdkPath + @"\Include\shared", vsPath + @"\include", monoPath + @"\include\mono-2.0", "." };
                        string[] libs          = new string[] { winsdkPath + @"\Lib\winv6.3\um\x86", vsPath + @"\lib" };
                        var      linkLibraries = new string[] { "kernel32.lib",
                                                                "version.lib",
                                                                "Ws2_32.lib",
                                                                "Mswsock.lib",
                                                                "Psapi.lib",
                                                                "shell32.lib",
                                                                "OleAut32.lib",
                                                                "ole32.lib",
                                                                "winmm.lib",
                                                                "user32.lib",
                                                                "libvcruntime.lib",
                                                                "advapi32.lib",
                                                                "OLDNAMES.lib",
                                                                "libucrt.lib" };

                        string glue_obj = "mkbundle_glue.obj";
                        string monoLib;

                        if (static_link)
                        {
                            monoLib = LocateFile(monoPath + @"\lib\monosgen-2.0-static.lib");
                        }

                        else
                        {
                            Console.WriteLine("WARNING: Dynamically linking the Mono runtime on Windows is not a tested option.");
                            monoLib = LocateFile(monoPath + @"\lib\monosgen-2.0.lib");
                            LocateFile(monoPath + @"\lib\monosgen-2.0.dll");              // in this case, the .lib is just the import library, and the .dll is also needed
                        }

                        var compilerArgs = new List <string>();
                        compilerArgs.Add("/MT");

                        foreach (string include in includes)
                        {
                            compilerArgs.Add(String.Format("/I {0}", quote(include)));
                        }

                        if (!nomain || custom_main != null)
                        {
                            compilerArgs.Add(quote(temp_c));
                            compilerArgs.Add(quote(temp_o));
                            if (custom_main != null)
                            {
                                compilerArgs.Add(quote(custom_main));
                            }
                            compilerArgs.Add(quote(monoLib));
                            compilerArgs.Add("/link");
                            compilerArgs.Add("/NODEFAULTLIB");
                            compilerArgs.Add("/SUBSYSTEM:windows");
                            compilerArgs.Add("/ENTRY:mainCRTStartup");
                            compilerArgs.AddRange(linkLibraries);
                            compilerArgs.Add("/out:" + output);

                            string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
                            Execute(cl_cmd);
                        }
                        else
                        {
                            // we are just creating a .lib
                            compilerArgs.Add("/c");                            // compile only
                            compilerArgs.Add(temp_c);
                            compilerArgs.Add(String.Format("/Fo" + glue_obj)); // .obj output name

                            string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
                            Execute(cl_cmd);

                            string librarian     = GetEnv("LIB", "lib.exe");
                            var    librarianArgs = new List <string> ();
                            librarianArgs.Add(String.Format("/out:{0}.lib" + output));
                            librarianArgs.Add(temp_o);
                            librarianArgs.Add(glue_obj);
                            librarianArgs.Add(monoLib);
                            string lib_cmd = String.Format("{0} {1}", librarian, String.Join(" ", librarianArgs.ToArray()));
                            Execute(lib_cmd);
                        }
                    }
                    else
                    {
                        string zlib      = (compress ? "-lz" : "");
                        string debugging = "-g";
                        string cc        = GetEnv("CC", "cc");
                        string cmd       = null;

                        if (style == "linux")
                        {
                            debugging = "-ggdb";
                        }
                        if (static_link)
                        {
                            string smonolib;
                            if (style == "osx")
                            {
                                smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
                            }
                            else
                            {
                                smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
                            }
                            cmd = String.Format("{4} -o '{2}' -Wall `pkg-config --cflags mono-2` {0} {3} " +
                                                "`pkg-config --libs-only-L mono-2` " + smonolib +
                                                "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
                                                temp_c, temp_o, output, zlib, cc);
                        }
                        else
                        {
                            cmd = String.Format("{4} " + debugging + " -o '{2}' -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
                                                temp_c, temp_o, output, zlib, cc);
                        }
                        Execute(cmd);
                    }

                    if (!quiet)
                    {
                        Console.WriteLine("Done");
                    }
                }
            }
        } finally {
            if (!keeptemp)
            {
                if (object_out == null)
                {
                    File.Delete(temp_o);
                }
                if (!compile_only)
                {
                    File.Delete(temp_c);
                }
                File.Delete(temp_s);
            }
        }
    }
예제 #27
0
        public bool SaveBackup(string fileName, RegionData regiondata)
        {
            try
            {
                bool oldFileExists = File.Exists(fileName);
                //Do new style saving here!
                GZipStream m_saveStream = new GZipStream(new FileStream(fileName + ".tmp", FileMode.Create),
                                                         CompressionMode.Compress);
                TarArchiveWriter writer       = new TarArchiveWriter(m_saveStream);
                GZipStream       m_loadStream = new GZipStream(new FileStream(fileName, FileMode.Open),
                                                               CompressionMode.Decompress);
                TarArchiveReader reader = new TarArchiveReader(m_loadStream);

                writer.WriteDir("parcels");

                foreach (LandData parcel in regiondata.Parcels)
                {
                    OSDMap parcelMap = parcel.ToOSD();
                    var    binary    = OSDParser.SerializeLLSDBinary(parcelMap);
                    writer.WriteFile("parcels/" + parcel.GlobalID.ToString(),
                                     binary);
                    binary    = null;
                    parcelMap = null;
                }

                writer.WriteDir("newstyleterrain");
                writer.WriteDir("newstylerevertterrain");

                writer.WriteDir("newstylewater");
                writer.WriteDir("newstylerevertwater");

                writer.WriteDir("regioninfo");
                byte[] regionData = OSDParser.SerializeLLSDBinary(regiondata.RegionInfo.PackRegionInfoData());
                writer.WriteFile("regioninfo/regioninfo", regionData);

                try
                {
                    writer.WriteFile("newstyleterrain/" + regiondata.RegionInfo.RegionID.ToString() + ".terrain",
                                     regiondata.Terrain);

                    writer.WriteFile(
                        "newstylerevertterrain/" + regiondata.RegionInfo.RegionID.ToString() + ".terrain",
                        regiondata.RevertTerrain);

                    if (regiondata.Water != null)
                    {
                        writer.WriteFile("newstylewater/" + regiondata.RegionInfo.RegionID.ToString() + ".terrain",
                                         regiondata.Water);

                        writer.WriteFile(
                            "newstylerevertwater/" + regiondata.RegionInfo.RegionID.ToString() + ".terrain",
                            regiondata.RevertWater);
                    }
                }
                catch (Exception ex)
                {
                    MainConsole.Instance.WarnFormat("[Backup]: Exception caught: {0}", ex);
                }

                List <UUID> entitiesToSave = new List <UUID>();
                foreach (ISceneEntity entity in regiondata.Groups)
                {
                    try
                    {
                        if (entity.IsAttachment ||
                            ((entity.RootChild.Flags & PrimFlags.Temporary) == PrimFlags.Temporary) ||
                            ((entity.RootChild.Flags & PrimFlags.TemporaryOnRez) == PrimFlags.TemporaryOnRez))
                        {
                            continue;
                        }
                        if (entity.HasGroupChanged || !oldFileExists)
                        {
                            entity.HasGroupChanged = false;
                            //Write all entities
                            writer.WriteFile("entities/" + entity.UUID.ToString(), entity.ToBinaryXml2());
                        }
                        else
                        {
                            entitiesToSave.Add(entity.UUID);
                        }
                    }
                    catch (Exception ex)
                    {
                        MainConsole.Instance.WarnFormat("[Backup]: Exception caught: {0}", ex);
                        entitiesToSave.Add(entity.UUID);
                    }
                }

                if (oldFileExists)
                {
                    byte[] data;
                    string filePath;
                    TarArchiveReader.TarEntryType entryType;
                    //Load the archive data that we need
                    try
                    {
                        while ((data = reader.ReadEntry(out filePath, out entryType)) != null)
                        {
                            if (TarArchiveReader.TarEntryType.TYPE_DIRECTORY == entryType)
                            {
                                continue;
                            }
                            if (filePath.StartsWith("entities/"))
                            {
                                UUID entityID = UUID.Parse(filePath.Remove(0, 9));
                                if (entitiesToSave.Contains(entityID))
                                {
                                    writer.WriteFile(filePath, data);
                                    entitiesToSave.Remove(entityID);
                                }
                            }
                            data = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        MainConsole.Instance.WarnFormat("[Backup]: Exception caught: {0}", ex);
                    }

                    if (entitiesToSave.Count > 0)
                    {
                        MainConsole.Instance.Fatal(entitiesToSave.Count +
                                                   " PRIMS WERE NOT GOING TO BE SAVED! FORCE SAVING NOW! ");
                        foreach (ISceneEntity entity in regiondata.Groups)
                        {
                            if (entitiesToSave.Contains(entity.UUID))
                            {
                                if (entity.IsAttachment ||
                                    ((entity.RootChild.Flags & PrimFlags.Temporary) == PrimFlags.Temporary) ||
                                    ((entity.RootChild.Flags & PrimFlags.TemporaryOnRez) == PrimFlags.TemporaryOnRez))
                                {
                                    continue;
                                }
                                //Write all entities
                                byte[] xml = entity.ToBinaryXml2();
                                writer.WriteFile("entities/" + entity.UUID.ToString(), xml);
                                xml = null;
                            }
                        }
                    }
                }

                reader.Close();
                writer.Close();
                m_loadStream.Close();
                m_saveStream.Close();
                GC.Collect();
            }
            catch (Exception ex)
            {
                MainConsole.Instance.Warn("[ProtobufRegionLoader]: Failed to save backup: " + ex.ToString());
                return(false);
            }
            return(true);
        }
예제 #28
0
        //------------------------------------------------------
        public sc2i.common.CResultAErreur Serialize(C2iSerializer serializer)
        {
            int            nVersion = GetNumVersion();
            CResultAErreur result   = serializer.TraiteVersion(ref nVersion);

            if (!result)
            {
                return(result);
            }

            //Serialisation de la table
            bool bHasTable = m_tableDesDifferences != null;

            serializer.TraiteBool(ref bHasTable);

            if (bHasTable)
            {
                if (serializer.Mode == ModeSerialisation.Ecriture)
                {
                    //Il faut sérialiser un dataset pour que
                    //Les rowState soient sauvegardés
                    DataSet ds = new DataSet();
                    ds.Tables.Add(m_tableDesDifferences);
                    //Serialise la table des différences
                    MemoryStream    s      = new MemoryStream();
                    GZipStream      szip   = new GZipStream(s, CompressionMode.Compress);
                    BinaryFormatter format = new BinaryFormatter();
                    format.Serialize(szip, ds);
                    szip.Close();
                    ds.Tables.Remove(m_tableDesDifferences);
                    byte[] data = s.GetBuffer();
                    serializer.TraiteByteArray(ref data);
                }
                if (serializer.Mode == ModeSerialisation.Lecture)
                {
                    byte[] data = null;
                    serializer.TraiteByteArray(ref data);
                    DataSet         ds     = new DataSet();
                    Stream          s      = new MemoryStream(data);
                    GZipStream      szip   = new GZipStream(s, CompressionMode.Decompress);
                    BinaryFormatter format = new BinaryFormatter();
                    try
                    {
                        ds = (DataSet)format.Deserialize(szip);
                        szip.Close();
                        m_tableDesDifferences = ds.Tables[0];
                        ds.Tables.Remove(m_tableDesDifferences);
                    }
                    catch
                    {
                        result.EmpileErreur(I.T("Error while reading datatable|545"));
                    }
                }
            }
            if (nVersion > 0)
            {
                serializer.TraiteBool(ref m_bChangementClePrimaire);
                serializer.TraiteBool(ref m_bChangementTypeTable);
            }
            if (nVersion > 1)
            {
                serializer.TraiteBool(ref m_bCreationTable);
                serializer.TraiteBool(ref m_bSuppressionTable);
            }
            return(result);
        }
예제 #29
0
        //restore database
        void restore(object file_name)
        {
            String tmpfile  = System.Environment.GetEnvironmentVariable("TEMP") + "\\stockrestore.sql";
            String filename = (String)file_name;

            RestoreProgress.Style = ProgressBarStyle.Marquee;
            try
            {
                FileInfo   fi       = new FileInfo(filename);
                FileStream inFile   = fi.OpenRead();        // open GZip file
                FileStream outFile  = File.Create(tmpfile); //open new SQL file
                GZipStream Compress = new GZipStream(inFile, CompressionMode.Decompress);
                int        len      = 0;
                while (Compress.ReadByte() != -1)
                {
                    len++; //get length of total text
                }
                Compress.Close();
                inFile.Close();
                inFile   = fi.OpenRead();
                Compress = new GZipStream(inFile, CompressionMode.Decompress);
                byte[] Data = new byte[len];         // create byte array length of toatl text
                Compress.Read(Data, 0, Data.Length); //read decompress data from GZip file to byte array
                outFile.Write(Data, 0, Data.Length); //read byte array to output SQL file
                outFile.Flush();
                outFile.Close();

                StreamReader file  = new StreamReader(tmpfile); //re-open SQL file for restore
                string       input = file.ReadToEnd();          //read all data to string
                file.Close();
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = "mysql"; //mysql exe file
                psi.RedirectStandardInput = true;
                psi.RedirectStandardError = true;
                psi.Arguments             = string.Format(@"-u{0} -p{1} -h{2} {3}", config.getValue("User"), config.getValue("Password"), config.getValue("Server"), config.getValue("Database"));
                psi.UseShellExecute       = false;
                Process process = Process.Start(psi); // start mysql process
                process.StandardInput.WriteLine(input);
                process.StandardInput.Close();
                process.WaitForExit();
                StreamReader stderr = process.StandardError;
                process.Close();
                string restext = stderr.ReadToEnd();
                if (restext != "")
                {
                    //if any stderror occurs when running mysql.exe
                    throw new Exception("unable to access server under current security context");
                }
                inFile.Close();
                File.Delete(tmpfile); // delete temp SQL file
                changeRestorePB();
                //Show message if success
                MessageBox.Show("Restore complete!", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information);
                isRestore = 1;     // set restore=1
            }
            catch (Win32Exception) // if error occurs when starting mysql.exe
            {
                changeRestorePB();
                MessageBox.Show("Mysql command-line tools not installed on system or you have not added the MySQL bin directory to your Windows PATH", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            catch (IOException) // if input/output error occurs
            {
                changeRestorePB();
                MessageBox.Show("a required file is either corrupted or not available.", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            catch (Exception ex) // if other error occurs
            {
                changeRestorePB();
                MessageBox.Show("Error:" + ex.Message, "Restore", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
예제 #30
0
파일: Compress.cs 프로젝트: QYFS2018/VC2
    public void DecompressFile(string sourceFile, string destinationFile)
    {
        try
        {
            sourceFile      = HttpContext.Current.Request.PhysicalApplicationPath + sourceFile;
            destinationFile = HttpContext.Current.Request.PhysicalApplicationPath + destinationFile;
        }
        catch { }

        // make sure the source file is there
        if (File.Exists(sourceFile) == false)
        {
            throw new FileNotFoundException();
        }

        // Create the streams and byte arrays needed
        FileStream sourceStream       = null;
        FileStream destinationStream  = null;
        GZipStream decompressedStream = null;

        byte[] quartetBuffer = null;

        try
        {
            // Read in the compressed source stream
            sourceStream = new FileStream(sourceFile, FileMode.Open);

            // Create a compression stream pointing to the destiantion stream
            decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);

            // Read the footer to determine the length of the destiantion file
            quartetBuffer = new byte[4];
            int position = (int)sourceStream.Length - 4;
            sourceStream.Position = position;
            sourceStream.Read(quartetBuffer, 0, 4);
            sourceStream.Position = 0;
            int checkLength = BitConverter.ToInt32(quartetBuffer, 0);

            byte[] buffer = new byte[checkLength + 100];

            int offset = 0;
            int total  = 0;

            // Read the compressed data into the buffer
            while (true)
            {
                int bytesRead = decompressedStream.Read(buffer, offset, 100);

                if (bytesRead == 0)
                {
                    break;
                }

                offset += bytesRead;
                total  += bytesRead;
            }

            // Now write everything to the destination file
            destinationStream = new FileStream(destinationFile, FileMode.Create);
            destinationStream.Write(buffer, 0, total);

            // and flush everyhting to clean out the buffer
            destinationStream.Flush();
        }
        catch (ApplicationException ex)
        {
            throw new ApplicationException(ex.ToString());
        }
        finally
        {
            // Make sure we allways close all streams
            if (sourceStream != null)
            {
                sourceStream.Close();
            }

            if (decompressedStream != null)
            {
                decompressedStream.Close();
            }

            if (destinationStream != null)
            {
                destinationStream.Close();
            }
        }
    }
예제 #31
0
파일: Class5.cs 프로젝트: daviastroaw/newts
 public static DataSet smethod_19(byte[] byte_0)
 {
     DataSet result;
     try
     {
         MemoryStream memoryStream = new MemoryStream(byte_0);
         GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
         DataSet dataSet = new DataSet();
         dataSet.ReadXml(gZipStream, XmlReadMode.ReadSchema);
         gZipStream.Close();
         memoryStream.Close();
         result = dataSet;
     }
     catch (Exception expr_2F)
     {
         ProjectData.SetProjectError(expr_2F);
         result = null;
         ProjectData.ClearProjectError();
     }
     return result;
 }
예제 #32
0
파일: Indexer.cs 프로젝트: Segachief/Godo
        // Old Method; retrieves camera data from the scene.bin before modification.
        // Contains methods for acquiring scene data for calculations/assignment
        //public static ArrayList GetCameraData(int[][] jaggedSceneInfo, string targetScene)
        //{
        //    int r = 0;
        //    int o = 0;
        //    ArrayList listedCameraData = new ArrayList();

        //    while (r < 256)
        //    {
        //        int bytesRead;
        //        byte[] uncompressedScene = new byte[7808]; // Used to hold the decompressed scene file

        //        using (BinaryReader brg = new BinaryReader(new FileStream(targetScene, FileMode.Open)))
        //        {
        //            // Calls method to convert little endian values into an integer
        //            byte[] compressedScene = new byte[jaggedSceneInfo[o][1]]; // Used to hold the compressed scene file, where [o][1] is scene size
        //            brg.BaseStream.Seek(jaggedSceneInfo[o][2], SeekOrigin.Begin); // Starts reading the compressed scene file
        //            brg.Read(compressedScene, 0, compressedScene.Length);

        //            using (MemoryStream inputWrapper = new MemoryStream(compressedScene))
        //            {
        //                using (MemoryStream decompressedOutput = new MemoryStream())
        //                {
        //                    using (GZipStream zipInput = new GZipStream(inputWrapper, CompressionMode.Decompress, true))
        //                    {
        //                        while ((bytesRead = zipInput.Read(uncompressedScene, 0, 7808)) != 0)
        //                        {
        //                            decompressedOutput.Write(uncompressedScene, 0, bytesRead);
        //                            // If this scene has valid camera data, then pull it out.
        //                            byte[] camera = new byte[48];
        //                            if (uncompressedScene[87] != 255 && uncompressedScene[88] != 255)
        //                            {
        //                                camera = uncompressedScene.Skip(88).Take(48).ToArray();
        //                                listedCameraData.Add(camera);
        //                            }
        //                        }
        //                        zipInput.Close();
        //                    }
        //                    decompressedOutput.Close();
        //                }
        //                inputWrapper.Close();
        //            }
        //            brg.Close();
        //        }
        //        r++;
        //        o++;
        //    }
        //    return listedCameraData;
        //}

        public static int[][][] GetAttackData(int[][] jaggedSceneInfo, string targetScene)
        {
            int r = 0;
            int o = 0;
            int c = 0;
            int k = 0;
            int y = 0;

            int[][]   jaggedAttackType       = new int[1024][];     // Attack ID > Attack Type - Used to derive jaggedModelAttackTypes
            int[][][] jaggedModelAttackTypes = new int[3000][][];   // Model ID > Attack Type > Animation Indices

            while (r < 256)
            {
                int    bytesRead;
                byte[] uncompressedScene = new byte[7808]; // Used to hold the decompressed scene file

                using (BinaryReader brg = new BinaryReader(new FileStream(targetScene, FileMode.Open)))
                {
                    // Calls method to convert little endian values into an integer
                    byte[] compressedScene = new byte[jaggedSceneInfo[o][1]];     // Used to hold the compressed scene file, where [o][1] is scene size
                    brg.BaseStream.Seek(jaggedSceneInfo[o][2], SeekOrigin.Begin); // Starts reading the compressed scene file
                    brg.Read(compressedScene, 0, compressedScene.Length);

                    using (MemoryStream inputWrapper = new MemoryStream(compressedScene))
                    {
                        using (MemoryStream decompressedOutput = new MemoryStream())
                        {
                            using (GZipStream zipInput = new GZipStream(inputWrapper, CompressionMode.Decompress, true))
                            {
                                while ((bytesRead = zipInput.Read(uncompressedScene, 0, 7808)) != 0)
                                {
                                    /* Step 1: Create an array with all AttackIDs and AttackTypes
                                     * To determine attack type, we check the Impact Effect ID (phys) and Attack Effect ID (mag).
                                     * If either are FF then we can assume it is the other type. If both are FF, it is a Misc.
                                     * 0 = Phys, 1 = Mag, 2 = Misc
                                     */

                                    while (c < 32) // Iterate through all 32 entries for attack data in the scene
                                    {
                                        decompressedOutput.Write(uncompressedScene, 0, bytesRead);
                                        byte[] attackID = new byte[2];
                                        int    type;

                                        // Checks AttackID isn't blank and then takes it, converts it into Int for array index
                                        if (uncompressedScene[2113 + k] != 255)
                                        {
                                            attackID = uncompressedScene.Skip(2112 + k).Take(2).ToArray();
                                            int attackIDInt = EndianConvert.GetLittleEndianIntTwofer(attackID, 0);

                                            // Checks anim and impact to determine attack type
                                            if (uncompressedScene[1217 + y] != 255)
                                            {
                                                type = 0; // Assigns this AttackID as Physical
                                                jaggedAttackType[attackIDInt] = new int[] { type };
                                            }
                                            else if (uncompressedScene[1229 + y] != 255)
                                            {
                                                type = 1; // Assigns this AttackID as Magic
                                                jaggedAttackType[attackIDInt] = new int[] { type };
                                            }
                                            else
                                            {
                                                type = 2; // Assigns this AttackID as Misc
                                                jaggedAttackType[attackIDInt] = new int[] { type };
                                            }
                                        }
                                        c++;
                                        k += 2;
                                        y += 28;
                                    }
                                    c = 0;
                                    k = 0;
                                    y = 0;


                                    /* Step 2: Create an array that has the ModelIDs and their valid animations sorted into AttackTypes
                                     * To build an array of valid animation indexes for an enemy, we need to get a record of what anim indexes
                                     * have already been set for each of its associated attacks.
                                     * 0 = Phys, 1 = Mag, 2 = Misc
                                     */

                                    int enemyCount = 0;
                                    while (enemyCount < 3) // Iterates through the 3 registerable enemy slots in this scene
                                    {
                                        decompressedOutput.Write(uncompressedScene, 0, bytesRead);
                                        byte[] modelID     = new byte[2];
                                        byte[] attackID    = new byte[2];
                                        byte[] animID      = new byte[1];
                                        int    attackCount = 0;

                                        // Checks if enemy ID is Null/FFFF
                                        if (uncompressedScene[c + 1] != 255)
                                        {
                                            modelID = uncompressedScene.Skip(c).Take(2).ToArray();
                                            int modelIDInt = EndianConvert.GetLittleEndianIntTwofer(modelID, 0);
                                            jaggedModelAttackTypes[modelIDInt] = new int[3][]; // 3 entries for 3 attack types
                                            int[] physAnims = new int[16];
                                            int[] magAnims  = new int[16];
                                            int[] miscAnims = new int[16];
                                            int   physCount = 0;
                                            int   magCount  = 0;
                                            int   miscCount = 0;

                                            while (attackCount < 16) // Iterates through the 16 registerable attack slots of this enemy
                                            {
                                                // Checks AttackID isn't blank and then takes it, converts it into Int for array index
                                                if (uncompressedScene[736 + k] != 255 && uncompressedScene[736 + k] != 255)
                                                {
                                                    attackID = uncompressedScene.Skip(736 + k).Take(2).ToArray();
                                                    int attackIDInt = EndianConvert.GetLittleEndianIntTwofer(attackID, 0);

                                                    // Checks if an Anim was set for this AttackID (99% of cases one will be)
                                                    if (uncompressedScene[720 + y] != 255)
                                                    {
                                                        animID = uncompressedScene.Skip(720 + y).Take(1).ToArray();

                                                        if (jaggedAttackType[attackIDInt][0] == 0) // Attack Type is physical
                                                        {
                                                            physAnims[physCount] = animID[0];
                                                            physCount++;
                                                        }
                                                        else if (jaggedAttackType[attackIDInt][0] == 1) // Attack type is magical
                                                        {
                                                            magAnims[magCount] = animID[0];
                                                            magCount++;
                                                        }
                                                        else if (jaggedAttackType[attackIDInt][0] == 2) // Attack type is miscellaneous
                                                        {
                                                            miscAnims[miscCount] = animID[0];
                                                            miscCount++;
                                                        }
                                                        else
                                                        {
                                                            MessageBox.Show("Error: An animation was not assigned correctly");
                                                        }
                                                    }
                                                }
                                                k += 2; // Tracks location of AttackID
                                                y++;    // Tracks location of Animation Indice
                                                attackCount++;
                                            }
                                            // Places the phys, mag, misc animations collected so far and places them into the jagged array
                                            jaggedModelAttackTypes[modelIDInt][0] = new int[] { physAnims[0], physAnims[1], physAnims[2], physAnims[3], physAnims[4], physAnims[5], physAnims[6], physAnims[7], physAnims[8], physAnims[9], physAnims[10], physAnims[11], physAnims[12], physAnims[13], physAnims[14], physAnims[15] };
                                            jaggedModelAttackTypes[modelIDInt][1] = new int[] { magAnims[0], magAnims[1], magAnims[2], magAnims[3], magAnims[4], magAnims[5], magAnims[6], magAnims[7], magAnims[8], magAnims[9], magAnims[10], magAnims[11], magAnims[12], magAnims[13], magAnims[14], magAnims[15] };
                                            jaggedModelAttackTypes[modelIDInt][2] = new int[] { miscAnims[0], miscAnims[1], miscAnims[2], miscAnims[3], miscAnims[4], miscAnims[5], miscAnims[6], miscAnims[7], miscAnims[8], miscAnims[9], miscAnims[10], miscAnims[11], miscAnims[12], miscAnims[13], miscAnims[14], miscAnims[15] };
                                        }
                                        else // No enemy, so we move to the data for the next one
                                        {
                                            k += 184; // Tracks location of AttackID
                                            y += 200; // Tracks location of Animation Indice
                                        }

                                        c          += 2; // Next enemy ID offset
                                        attackCount = 0;
                                        enemyCount++;
                                        k += 152;
                                        y += 168;
                                    }
                                    c = 0;
                                    k = 0;
                                    y = 0;
                                }
                                zipInput.Close();
                            }
                            decompressedOutput.Close();
                        }
                        inputWrapper.Close();
                    }
                    brg.Close();
                }
                r++;
                o++;
            }
            return(jaggedModelAttackTypes);
        }
예제 #33
0
        /// <summary>
        ///
        /// </summary>
        /// <see cref="IWcfHttpServer.Request(Message)" />
        public Message Request(Message message)
        {
            using (var uncompressedResponse = new MemoryStream())
            {
                var request  = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];
                var response = new HttpResponseMessageProperty();

                // HTTP-Methode: bspw. GET oder POST
                var method = request.Method;

                // Kopfdaten der Anfrage
                var requestHeaders = new Dictionary <string, string>();
                foreach (var key in request.Headers.AllKeys)
                {
                    requestHeaders[key] = request.Headers[key];
                }

                // Rohdaten der Anfrage (nur Body) ermitteln
                byte[] requestBody;
                using (var requestStream = new MemoryStream())
                {
                    this._WEB_ENCODER.WriteMessage(message, requestStream);

                    requestBody = requestStream.ToArray();
                }

                // Beispiel: Antwort definieren
                byte[] responseData;
                {
                    // eigene Kopfdaten definieren
                    {
                        //TODO: Dictionary füllen
                        var responseHeaders = new Dictionary <string, string>();

                        foreach (var item in responseHeaders)
                        {
                            response.Headers[item.Key] = item.Value;
                        }
                    }

                    // Beispiel HTML-Ausgabe
                    {
                        var html = new StringBuilder().Append("<html>")
                                   .Append("<body>")
                                   .AppendFormat("Hallo, es ist: {0}",
                                                 DateTimeOffset.Now)
                                   .Append("</body>")
                                   .Append("</html>");

                        var utf8Html = Encoding.UTF8
                                       .GetBytes(html.ToString());

                        uncompressedResponse.Write(utf8Html, 0, utf8Html.Length);

                        response.Headers[HttpResponseHeader.ContentType]
                            = "text/html; charset=utf-8";
                    }

                    // komprimieren?
                    var compress = true;
                    if (compress)
                    {
                        // mit GZIP komprimieren

                        using (var compressedResponse = new MemoryStream())
                        {
                            using (var gzip = new GZipStream(compressedResponse,
                                                             CompressionMode.Compress))
                            {
                                long oldPos = uncompressedResponse.Position;
                                try
                                {
                                    uncompressedResponse.Position = 0;

                                    var buffer = new byte[81920];
                                    int bytesRead;
                                    while ((bytesRead = uncompressedResponse.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        gzip.Write(buffer, 0, bytesRead);
                                    }
                                }
                                finally
                                {
                                    uncompressedResponse.Position = oldPos;
                                }

                                gzip.Flush();
                                gzip.Close();

                                responseData = compressedResponse.ToArray();
                            }
                        }

                        response.Headers[HttpResponseHeader.ContentEncoding] = "gzip";
                    }
                    else
                    {
                        responseData = uncompressedResponse.ToArray();
                    }
                }

                // HTTP-Status Code (hier: 200)
                response.StatusCode = HttpStatusCode.OK;

                // WCF-Antwort erstellen
                var responseMessage = new BinaryMessage(responseData);
                responseMessage.Properties[HttpResponseMessageProperty.Name] = response;

                return(responseMessage);
            }
        }
예제 #34
0
    protected override void Execute()
    {
        string skey1 = web.Param("skey1");

        if (skey1 != SERVER_API_KEY)
        {
            throw new ApiExitException("bad key");
        }

        string sessionId = web.Param("sessionId");

        HttpPostedFile logFile = this.Request.Files["logFile"];

        if (logFile == null)
        {
            throw new ApiExitException("no logFile");
        }
        int logSize = Convert.ToInt32(web.Param("logSize"));

        GZipStream GZip = new GZipStream(logFile.InputStream, CompressionMode.Decompress);

        Byte[] logData = new Byte[logSize];
        GZip.Read(logData, 0, logSize);
        GZip.Close();

        // create log dir
        string dir = string.Format("c:\\inetpub\\logss\\{0}-{1:00}-{2:00}",
                                   DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

        System.IO.Directory.CreateDirectory(dir);

        // Open file for writing
        string     fname = string.Format("{0}\\GS_{1}.txt", dir, sessionId);
        FileStream fs    = new FileStream(fname, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        fs.Write(logData, 0, logData.Length);
        fs.Close();

        // check if we have crash dump file
        HttpPostedFile dmpFile = this.Request.Files["dmpFile"];

        if (dmpFile != null)
        {
            int dmpSize = Convert.ToInt32(web.Param("dmpSize"));

            GZip = new GZipStream(dmpFile.InputStream, CompressionMode.Decompress);
            Byte[] dmpData = new Byte[dmpSize];
            GZip.Read(dmpData, 0, dmpSize);
            GZip.Close();

            // create crash logs dir
            dir = string.Format("c:\\inetpub\\logss_CRASH");
            System.IO.Directory.CreateDirectory(dir);

            // save log there
            fname = string.Format("{0}\\GS_{1}.txt", dir, sessionId);
            fs    = new FileStream(fname, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            fs.Write(logData, 0, logData.Length);
            fs.Close();

            // save dump there
            fname = string.Format("{0}\\GS_{1}.dmp", dir, sessionId);
            fs    = new FileStream(fname, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            fs.Write(dmpData, 0, dmpData.Length);
            fs.Close();
        }

        WriteAttachedImages();

        Response.Write("WO_0");
    }
예제 #35
0
    /// <summary>
    /// Encodes one file to another file that is gzip compressed.
    /// File is overwritten if it exists and not locked.
    /// </summary>
    /// <param name="Filename"></param>
    /// <param name="OutputFile"></param>
    /// <returns></returns>
    public static bool GZipFile(string Filename, string OutputFile)
    {
        string InputFile = Filename;
        byte[] Buffer = File.ReadAllBytes(Filename);
        FileStream fs = new FileStream(OutputFile, FileMode.OpenOrCreate, FileAccess.Write);
        GZipStream GZip = new GZipStream(fs, CompressionMode.Compress);
        GZip.Write(Buffer, 0, Buffer.Length);
        GZip.Close();
        fs.Close();

        return true;
    }
예제 #36
0
        private bool SaveFile(string fileName)
        {
            if (_workingDir == null || (!this.SaveXaml && !this.SaveZaml))
            {
                return(false);
            }

            _writerErrorOccurred = false;

            string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);

            string xamlFileName = Path.Combine(_workingDir.FullName,
                                               fileNameWithoutExt + ".xaml");

            if (File.Exists(xamlFileName))
            {
                File.SetAttributes(xamlFileName, FileAttributes.Normal);
                File.Delete(xamlFileName);
            }

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent             = true;
                writerSettings.OmitXmlDeclaration = true;
                writerSettings.Encoding           = Encoding.UTF8;
                using (FileStream xamlFile = File.Create(xamlFileName))
                {
                    using (XmlWriter writer = XmlWriter.Create(xamlFile, writerSettings))
                    {
                        System.Windows.Markup.XamlWriter.Save(_drawing, writer);
                    }
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(this.DrawingSettings);

                    using (FileStream xamlFile = File.Create(xamlFileName))
                    {
                        xamlWriter.Save(_drawing, xamlFile);
                    }
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        // If the file exist, we back it up and save a new file...
                        if (File.Exists(xamlFileName))
                        {
                            File.Move(xamlFileName, xamlFileName + ".bak");
                        }

                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent             = true;
                        writerSettings.OmitXmlDeclaration = true;
                        writerSettings.Encoding           = Encoding.UTF8;
                        using (FileStream xamlFile = File.Create(xamlFileName))
                        {
                            using (XmlWriter writer = XmlWriter.Create(xamlFile, writerSettings))
                            {
                                System.Windows.Markup.XamlWriter.Save(_drawing, writer);
                            }
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (this.SaveZaml)
            {
                string zamlFileName = Path.ChangeExtension(xamlFileName, ".zaml");

                if (File.Exists(zamlFileName))
                {
                    File.SetAttributes(zamlFileName, FileAttributes.Normal);
                    File.Delete(zamlFileName);
                }

                FileStream zamlSourceFile = new FileStream(
                    xamlFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[zamlSourceFile.Length];
                // Read the file to ensure it is readable.
                int count = zamlSourceFile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    zamlSourceFile.Close();
                    return(false);
                }
                zamlSourceFile.Close();

                FileStream zamlDestFile = File.Create(zamlFileName);

                GZipStream zipStream = new GZipStream(zamlDestFile,
                                                      CompressionMode.Compress, true);
                zipStream.Write(buffer, 0, buffer.Length);

                zipStream.Close();

                zamlDestFile.Close();

                _zamlFile = zamlFileName;
            }
            _xamlFile = xamlFileName;

            if (!this.SaveXaml && File.Exists(xamlFileName))
            {
                File.Delete(xamlFileName);
                _xamlFile = null;
            }

            return(true);
        }
예제 #37
0
파일: Parser.cs 프로젝트: xulei-ssh/LC-List
        public static void GenReport(string filename, int startIndex, string fontName)
        {
            Dictionary <string, string> syn = GenSyn();
            FileStream   srcFs    = File.OpenRead(filename);
            XmlDocument  dataFile = new XmlDocument();
            GZipStream   g        = new GZipStream(srcFs, CompressionMode.Decompress);
            MemoryStream ms       = new MemoryStream();

            byte[] bytes = new byte[40960];
            int    n;

            while ((n = g.Read(bytes, 0, bytes.Length)) > 0)
            {
                ms.Write(bytes, 0, n);
            }
            g.Close();
            ms.Position = 0;
            byte[] eData = ms.ToArray();
            for (int i = 0; i < eData.Length; i++)
            {
                eData[i] = (byte)(eData[i] + i);
            }
            ms = new MemoryStream(eData);
            dataFile.Load(ms);
            srcFs.Close();
            XmlNode       root     = dataFile.SelectSingleNode("ListDocument");
            var           projName = root.SelectSingleNode("Proj").Attributes["name"].InnerText;
            List <string> items    = new List <string>();

            foreach (XmlNode i in root.SelectSingleNode("ItemList").ChildNodes)
            {
                items.Add(i.Attributes["name"].InnerText);
            }
            List <string> lots = new List <string>();

            foreach (XmlNode i in root.SelectSingleNode("Lots").ChildNodes)
            {
                lots.Add(i.Attributes["l"].InnerText);
            }
            Dictionary <string, string> STD = new Dictionary <string, string>();

            foreach (XmlNode i in root.SelectSingleNode("STD").ChildNodes)
            {
                STD.Add(i.Attributes["suf"].InnerText, i.Attributes["item"].InnerText);
            }
            List <ListInj> injs = new List <ListInj>();

            foreach (XmlNode i in root.SelectSingleNode("List").ChildNodes)
            {
                injs.Add(new ListInj(int.Parse(i.Attributes["count"].InnerText), i.Attributes["name"].InnerText));
            }
            injs = SimplifyList(injs);
            // Below is writing docx file using items, lots, STD, injs
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CN");
            string temp          = Environment.GetEnvironmentVariable("TEMP");
            Random rnd           = new Random();
            string shortFileName = filename.Substring(filename.LastIndexOf("\\") + 1, filename.Length - filename.LastIndexOf("\\") - 5);

            if (temp == "")
            {
                temp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + shortFileName + "_" + rnd.Next(0, 500).ToString() + ".docx";
            }
            else
            {
                temp = temp + "\\" + shortFileName + "_" + rnd.Next(0, 500).ToString() + ".docx";
            }
            using (DocX document = DocX.Create(temp, DocumentTypes.Document))
            {
                //Start of title
                string title = projName;
                foreach (var i in items)
                {
                    title += syn[i] + "、";
                }
                title  = title.Substring(0, title.Length - 1);
                title += "测定";
                document.InsertParagraph(title).FontSize(16d).SpacingAfter(8).Font(new Font(fontName)).Alignment = Alignment.center;
                // End of Title (OK)
                // Start of Lot
                var p = document.InsertParagraph();
                // Append some text and add formatting.
                p.Append("批号:\t").Font(new Font(fontName)).FontSize(13);
                p.Append(ParseLotReport(lots).Replace("[and_placeholder]", "&")).Font(new Font(fontName)).FontSize(13);
                //End of Lot
                //Start of Table
                var t        = document.AddTable(1, 3);
                var colWidth = new float[] { 180f, 700f, 150f };
                t.SetWidths(colWidth);
                t.Alignment = Alignment.center;
                t.Design    = TableDesign.TableGrid;
                WordPlugin.Formatting f = new WordPlugin.Formatting();
                f.FontFamily = new Font(fontName);
                f.Size       = 13;
                t.Rows[0].Cells[0].Paragraphs[0].Append("图谱编号", f);
                t.Rows[0].Cells[0].VerticalAlignment = VerticalAlignment.Center;
                t.Rows[0].Cells[1].Paragraphs[0].Append("图谱内容", f);
                t.Rows[0].Cells[1].VerticalAlignment = VerticalAlignment.Center;
                t.Rows[0].Cells[2].Paragraphs[0].Append("备注", f);
                t.Rows[0].Cells[2].VerticalAlignment = VerticalAlignment.Center;
                if (startIndex != 1)
                {
                    injs.Insert(0, new ListInj(startIndex - 1, ""));
                }
                startIndex = 1;
                foreach (var inj in injs)
                {
                    string strIndex = startIndex.ToString();
                    if (inj.Count != 1)
                    {
                        strIndex += "-";
                    }
                    startIndex += inj.Count;
                    if (inj.Count != 1)
                    {
                        strIndex += (startIndex - 1).ToString();
                    }
                    var c = t.InsertRow();
                    c.Cells[0].Paragraphs[0].Append(strIndex, f);
                    c.Cells[0].VerticalAlignment = VerticalAlignment.Center;
                    c.Cells[1].Paragraphs[0].Append(inj.Name == "" ? "" : ParseInjName(inj.Name).Replace("[and_placeholder]", "&"), f);
                    c.Cells[1].VerticalAlignment = VerticalAlignment.Center;
                    c.Cells[2].Paragraphs[0].Append("--", f);
                    c.Cells[2].VerticalAlignment = VerticalAlignment.Center;
                }
                document.InsertParagraph().InsertTableAfterSelf(t);
                document.Save();
            }
            System.Diagnostics.Process.Start("winword.exe", temp);
        }
예제 #38
0
        private void decompressFileAsync(string srcFile, string destFile, AsyncOperation asyOp)
        {
            FileStream fsIn = null, fsOut = null;
            GZipStream decompressGZipStream = null;

            try
            {
                //解压
                fsIn  = new FileStream(srcFile, FileMode.Open);
                fsOut = new FileStream(destFile, FileMode.Create);

                long   lSize     = fsIn.Length;
                int    hasRead   = -1;
                int    totalRead = 0;
                byte[] buffer    = new byte[BUFSIZE];

                decompressGZipStream = new GZipStream(fsIn, CompressionMode.Decompress);

                //报告内容
                int totalNumber = (int)lSize / BUFSIZE;
                while ((hasRead = decompressGZipStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    fsOut.Write(buffer, 0, hasRead);
                    totalRead += hasRead;

                    //报告进度,由于解压无法知道解压进度,所以当读取量为压缩文件大小时,置为100%
                    int curNumber = totalRead / BUFSIZE < totalNumber ? totalRead / BUFSIZE : totalNumber;
                    asyOp.Post(onTaskStateChangedReportDelgate, new TaskStateChangedEventArgs()
                    {
                        CryptState    = CryptState.Decompress,
                        Description   = "解压中",
                        TaskID        = 0,
                        CurrentNumber = curNumber,
                        TotalNumber   = totalNumber
                    });

                    //如果取消,则抛出取消异常
                    if (token.IsCancellationRequested)
                    {
                        throw new OperationCanceledException(token);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (decompressGZipStream != null)
                {
                    decompressGZipStream.Close();
                }
                if (fsIn != null)
                {
                    fsIn.Close();
                }
                if (fsOut != null)
                {
                    fsOut.Close();
                }
            }
            return;
        }
예제 #39
0
파일: Types.cs 프로젝트: mbsky/kub-engine
    private static string GetPostedValue(XmlNode param, HttpContext context)
    {
        string value = null;
        string nm = param.Attributes["postparams"].Value;
        if (nm.Length == 0) nm = param.Name;

        if (param.Attributes["base64"] != null)
        {
            string b64 = param.Attributes["base64"].Value;
            byte[] data = System.Convert.FromBase64String(context.Request.Params[nm].Replace(' ', '+'));

            if (b64 == "gzip")
            {
                MemoryStream input = new MemoryStream(data);
                BinaryReader reader = new BinaryReader(input);

                int comp = reader.ReadInt32();
                int origin = reader.ReadInt32();

                byte[] rs = new byte[origin];

                if (comp < origin)
                {
                    GZipStream zipStream = new GZipStream(input, CompressionMode.Decompress);
                    ReadAllBytesFromStream(zipStream, rs);
                    zipStream.Close();
                }
                else ReadAllBytesFromStream(input, rs);

                input.Close();
                data = rs;
            }
            value = System.Text.Encoding.UTF8.GetString(data);
        }
        else value = context.Request.Params[nm];
        
        return value;
    }
예제 #40
0
        static void Main(string[] args)
        {
            // define an array of strings
            var callsigns = new string[] { "Husker", "Starbuck", "Apollo", "Boomer", "Bulldog", "Athena", "Helo", "Racetrack" };

            // define a file to write to using a text writer helper
            var          textFile = @"C:\Code\Ch06_Streams.txt";
            StreamWriter text     = File.CreateText(textFile);

            // enumerate the strings writing each one to the stream
            foreach (var item in callsigns)
            {
                text.WriteLine(item);
            }
            text.Close(); // close the stream

            // output all the contents of the file to the Console
            WriteLine($"{textFile} contains {new FileInfo(textFile).Length} bytes.");
            WriteLine(File.ReadAllText(textFile));

            // define a file to write to using the XML writer helper
            var       xmlFile = @"C:\Code\Ch06_Streams.xml";
            XmlWriter xml     = XmlWriter.Create(xmlFile, new XmlWriterSettings {
                Indent = true
            });

            // write the XML declaration
            xml.WriteStartDocument();

            // write a root element
            xml.WriteStartElement("callsigns");

            // enumerate the strings writing each one to the stream
            foreach (var item in callsigns)
            {
                xml.WriteElementString("callsign", item);
            }

            // write the close root element
            xml.WriteEndElement();
            xml.Close();

            // output all the contents of the file to the Console
            WriteLine($"{xmlFile} contains {new FileInfo(xmlFile).Length} bytes.");
            WriteLine(File.ReadAllText(xmlFile));

            // compress the XML output
            var gzipFilePath = @"C:\Code\Ch06.gzip";
            var gzipFile     = File.Create(gzipFilePath);
            var compressor   = new GZipStream(gzipFile, CompressionMode.Compress);
            var xmlGzip      = XmlWriter.Create(compressor);

            xmlGzip.WriteStartDocument();
            xmlGzip.WriteStartElement("callsigns");
            foreach (var item in callsigns)
            {
                xmlGzip.WriteElementString("callsign", item);
            }
            xmlGzip.Close();
            compressor.Close(); // also closes the underlying stream

            // output all the contents of the compressed file to the Console
            WriteLine($"{gzipFilePath} contains {new FileInfo(gzipFilePath).Length} bytes.");
            WriteLine(File.ReadAllText(gzipFilePath));

            // read a compressed file
            WriteLine("Reading the compressed XML file:");
            gzipFile = File.Open(gzipFilePath, FileMode.Open);
            var decompressor = new GZipStream(gzipFile, CompressionMode.Decompress);
            var reader       = XmlReader.Create(decompressor);

            while (reader.Read())
            {
                // check if we are currently on an element node named callsign
                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "callsign"))
                {
                    reader.Read();                // move to the Text node inside the element
                    WriteLine($"{reader.Value}"); // read its value
                }
            }
            reader.Close();
            decompressor.Close();
        }
예제 #41
0
        public override void Use(Player p, string message)
        {
            string path;

            if (message.Split(' ').Length == 1)
            {
                path = "levels/" + message + ".lvl";
            }
            else if (message.Split(' ').Length == 2)
            {
                try { path = @Server.backupLocation + "/" + message.Split(' ')[0] + "/" + int.Parse(message.Split(' ')[1]) + "/" + message.Split(' ')[0] + ".lvl"; }
                catch { Help(p); return; }
            }
            else
            {
                Help(p); return;
            }

            if (File.Exists(path))
            {
                FileStream fs = File.OpenRead(path);
                try
                {
                    GZipStream gs  = new GZipStream(fs, CompressionMode.Decompress);
                    byte[]     ver = new byte[2];
                    gs.Read(ver, 0, ver.Length);
                    ushort version = BitConverter.ToUInt16(ver, 0);
                    Level  level;
                    if (version == 1874)
                    {
                        byte[] header = new byte[16]; gs.Read(header, 0, header.Length);
                        ushort width  = BitConverter.ToUInt16(header, 0);
                        ushort height = BitConverter.ToUInt16(header, 2);
                        ushort depth  = BitConverter.ToUInt16(header, 4);
                        level        = new Level(name, width, depth, height, "empty");
                        level.spawnx = BitConverter.ToUInt16(header, 6);
                        level.spawnz = BitConverter.ToUInt16(header, 8);
                        level.spawny = BitConverter.ToUInt16(header, 10);
                        level.rotx   = header[12]; level.roty = header[13];
                    }
                    else
                    {
                        byte[] header = new byte[12]; gs.Read(header, 0, header.Length);
                        ushort width  = version;
                        ushort height = BitConverter.ToUInt16(header, 0);
                        ushort depth  = BitConverter.ToUInt16(header, 2);
                        level        = new Level(name, width, depth, height, "grass");
                        level.spawnx = BitConverter.ToUInt16(header, 4);
                        level.spawnz = BitConverter.ToUInt16(header, 6);
                        level.spawny = BitConverter.ToUInt16(header, 8);
                        level.rotx   = header[10]; level.roty = header[11];
                    }

                    level.setPhysics(0);

                    byte[] blocks = new byte[level.width * level.height * level.depth];
                    gs.Read(blocks, 0, blocks.Length);
                    level.blocks = blocks;
                    gs.Close();

                    level.backedup        = true;
                    level.permissionbuild = LevelPermission.Admin;

                    level.jailx    = (ushort)(level.spawnx * 32); level.jaily = (ushort)(level.spawny * 32); level.jailz = (ushort)(level.spawnz * 32);
                    level.jailrotx = level.rotx; level.jailroty = level.roty;

                    p.Loading = true;
                    foreach (Player pl in Player.players)
                    {
                        if (p.level == pl.level && p != pl)
                        {
                            p.SendDie(pl.id);
                        }
                    }
                    foreach (PlayerBot b in PlayerBot.playerbots)
                    {
                        if (p.level == b.level)
                        {
                            p.SendDie(b.id);
                        }
                    }

                    Player.GlobalDie(p, true);

                    p.level = level;
                    p.SendMotd();

                    p.SendRaw(2);
                    byte[] buffer = new byte[level.blocks.Length + 4];
                    BitConverter.GetBytes(IPAddress.HostToNetworkOrder(level.blocks.Length)).CopyTo(buffer, 0);
                    //ushort xx; ushort yy; ushort zz;

                    for (int i = 0; i < level.blocks.Length; ++i)
                    {
                        buffer[4 + i] = Block.Convert(level.blocks[i]);
                    }

                    buffer = Player.GZip(buffer);
                    int number = (int)Math.Ceiling(((double)buffer.Length) / 1024);
                    for (int i = 1; buffer.Length > 0; ++i)
                    {
                        short  length = (short)Math.Min(buffer.Length, 1024);
                        byte[] send   = new byte[1027];
                        Player.HTNO(length).CopyTo(send, 0);
                        Buffer.BlockCopy(buffer, 0, send, 2, length);
                        byte[] tempbuffer = new byte[buffer.Length - length];
                        Buffer.BlockCopy(buffer, length, tempbuffer, 0, buffer.Length - length);
                        buffer     = tempbuffer;
                        send[1026] = (byte)(i * 100 / number);
                        p.SendRaw(3, send);
                        Thread.Sleep(10);
                    }
                    buffer = new byte[6];
                    Player.HTNO((short)level.width).CopyTo(buffer, 0);
                    Player.HTNO((short)level.depth).CopyTo(buffer, 2);
                    Player.HTNO((short)level.height).CopyTo(buffer, 4);
                    p.SendRaw(4, buffer);

                    ushort x = (ushort)((0.5 + level.spawnx) * 32);
                    ushort y = (ushort)((1 + level.spawny) * 32);
                    ushort z = (ushort)((0.5 + level.spawnz) * 32);

                    p.aiming = false;
                    Player.GlobalSpawn(p, x, y, z, level.rotx, level.roty, true);
                    p.ClearBlockchange();
                    p.Loading = false;

                    if (message.IndexOf(' ') == -1)
                    {
                        level.name = "&cMuseum " + Server.DefaultColor + "(" + message.Split(' ')[0] + ")";
                    }
                    else
                    {
                        level.name = "&cMuseum " + Server.DefaultColor + "(" + message.Split(' ')[0] + " " + message.Split(' ')[1] + ")";
                    }

                    if (!p.hidden)
                    {
                        Player.GlobalChat(null, p.color + p.prefix + p.name + Server.DefaultColor + " went to the " + level.name, false);
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                catch (Exception ex) { Player.SendMessage(p, "Error loading level."); Server.ErrorLog(ex); return; }
                finally { fs.Close(); }
            }
            else
            {
                Player.SendMessage(p, "Level or backup could not be found."); return;
            }
        }
예제 #42
0
    /// <summary>
    /// Zips the file
    /// </summary>
    public void Zip()
    {
        byte[] byteArray = null;
        byteArray = GetRawFile(Url);

        //Prepare for compress
        using (MemoryStream ms = new MemoryStream())
        using (GZipStream sw = new GZipStream(ms, CompressionMode.Compress))
        {

            //Compress
            sw.Write(byteArray, 0, byteArray.Length);
            //Close, DO NOT FLUSH cause bytes will go missing...
            sw.Close();

            byteArray = ms.ToArray();
            ByteArrayToFile(string.Format("{0}{1}", BaseServerPath, FileName), byteArray);
        }
        FileLink = VirtualPathUtility.ToAbsolute(System.Configuration.ConfigurationManager.AppSettings["TEMPPATH"]) + FileName;
    }
예제 #43
0
    static void GenerateBundles(List <string> files)
    {
        string temp_s = "temp.s";         // Path.GetTempFileName ();
        string temp_c = "temp.c";
        string temp_o = "temp.o";

        if (compile_only)
        {
            temp_c = output;
        }
        if (object_out != null)
        {
            temp_o = object_out;
        }

        try {
            List <string>   c_bundle_names = new List <string> ();
            List <string[]> config_names   = new List <string[]> ();
            byte []         buffer         = new byte [8192];

            using (StreamWriter ts = new StreamWriter(File.Create(temp_s))) {
                using (StreamWriter tc = new StreamWriter(File.Create(temp_c))) {
                    string prog = null;

                    tc.WriteLine("/* This source code was produced by mkbundle, do not edit */");
                    tc.WriteLine("#include <mono/metadata/mono-config.h>");
                    tc.WriteLine("#include <mono/metadata/assembly.h>\n");

                    if (compress)
                    {
                        tc.WriteLine("typedef struct _compressed_data {");
                        tc.WriteLine("\tMonoBundledAssembly assembly;");
                        tc.WriteLine("\tint compressed_size;");
                        tc.WriteLine("} CompressedAssembly;\n");
                    }

                    object monitor = new object();

                    var streams = new Dictionary <string, Stream> ();
                    var sizes   = new Dictionary <string, long> ();

                    // Do the file reading and compression in parallel
                    Action <string> body = delegate(string url) {
                        string fname  = new Uri(url).LocalPath;
                        Stream stream = File.OpenRead(fname);

                        long real_size = stream.Length;
                        int  n;
                        if (compress)
                        {
                            MemoryStream ms      = new MemoryStream();
                            GZipStream   deflate = new GZipStream(ms, CompressionMode.Compress, leaveOpen: true);
                            while ((n = stream.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                deflate.Write(buffer, 0, n);
                            }
                            stream.Close();
                            deflate.Close();
                            byte [] bytes = ms.GetBuffer();
                            stream = new MemoryStream(bytes, 0, (int)ms.Length, false, false);
                        }

                        lock (monitor) {
                            streams [url] = stream;
                            sizes [url]   = real_size;
                        }
                    };

                    //#if NET_4_5
#if FALSE
                    Parallel.ForEach(files, body);
#else
                    foreach (var url in files)
                    {
                        body(url);
                    }
#endif

                    // The non-parallel part
                    foreach (var url in files)
                    {
                        string fname   = new Uri(url).LocalPath;
                        string aname   = Path.GetFileName(fname);
                        string encoded = aname.Replace("-", "_").Replace(".", "_");

                        if (prog == null)
                        {
                            prog = aname;
                        }

                        var stream    = streams [url];
                        var real_size = sizes [url];

                        Console.WriteLine("   embedding: " + fname);

                        WriteSymbol(ts, "assembly_data_" + encoded, stream.Length);

                        WriteBuffer(ts, stream, buffer);

                        if (compress)
                        {
                            tc.WriteLine("extern const unsigned char assembly_data_{0} [];", encoded);
                            tc.WriteLine("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
                                         " assembly_data_{0}, {2}}}, {3}}};",
                                         encoded, aname, real_size, stream.Length);
                            double ratio = ((double)stream.Length * 100) / real_size;
                            Console.WriteLine("   compression ratio: {0:.00}%", ratio);
                        }
                        else
                        {
                            tc.WriteLine("extern const unsigned char assembly_data_{0} [];", encoded);
                            tc.WriteLine("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
                                         encoded, aname, real_size);
                        }
                        stream.Close();

                        c_bundle_names.Add("assembly_bundle_" + encoded);

                        try {
                            FileStream cf = File.OpenRead(fname + ".config");
                            Console.WriteLine(" config from: " + fname + ".config");
                            tc.WriteLine("extern const unsigned char assembly_config_{0} [];", encoded);
                            WriteSymbol(ts, "assembly_config_" + encoded, cf.Length);
                            WriteBuffer(ts, cf, buffer);
                            ts.WriteLine();
                            config_names.Add(new string[] { aname, encoded });
                        } catch (FileNotFoundException) {
                            /* we ignore if the config file doesn't exist */
                        }
                    }

                    if (config_file != null)
                    {
                        FileStream conf;
                        try {
                            conf = File.OpenRead(config_file);
                        } catch {
                            Error(String.Format("Failure to open {0}", config_file));
                            return;
                        }
                        Console.WriteLine("System config from: " + config_file);
                        tc.WriteLine("extern const char system_config;");
                        WriteSymbol(ts, "system_config", config_file.Length);

                        WriteBuffer(ts, conf, buffer);
                        // null terminator
                        ts.Write("\t.byte 0\n");
                        ts.WriteLine();
                    }

                    if (machine_config_file != null)
                    {
                        FileStream conf;
                        try {
                            conf = File.OpenRead(machine_config_file);
                        } catch {
                            Error(String.Format("Failure to open {0}", machine_config_file));
                            return;
                        }
                        Console.WriteLine("Machine config from: " + machine_config_file);
                        tc.WriteLine("extern const char machine_config;");
                        WriteSymbol(ts, "machine_config", machine_config_file.Length);

                        WriteBuffer(ts, conf, buffer);
                        ts.Write("\t.byte 0\n");
                        ts.WriteLine();
                    }
                    ts.Close();

                    Console.WriteLine("Compiling:");
                    string cmd = String.Format("{0} -o {1} {2} ", GetEnv("AS", "as"), temp_o, temp_s);
                    int    ret = Execute(cmd);
                    if (ret != 0)
                    {
                        Error("[Fail]");
                        return;
                    }

                    if (compress)
                    {
                        tc.WriteLine("\nstatic const CompressedAssembly *compressed [] = {");
                    }
                    else
                    {
                        tc.WriteLine("\nstatic const MonoBundledAssembly *bundled [] = {");
                    }

                    foreach (string c in c_bundle_names)
                    {
                        tc.WriteLine("\t&{0},", c);
                    }
                    tc.WriteLine("\tNULL\n};\n");
                    tc.WriteLine("static char *image_name = \"{0}\";", prog);

                    tc.WriteLine("\nstatic void install_dll_config_files (void) {\n");
                    foreach (string[] ass in config_names)
                    {
                        tc.WriteLine("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
                    }
                    if (config_file != null)
                    {
                        tc.WriteLine("\tmono_config_parse_memory (&system_config);\n");
                    }
                    if (machine_config_file != null)
                    {
                        tc.WriteLine("\tmono_register_machine_config (&machine_config);\n");
                    }
                    tc.WriteLine("}\n");

                    if (config_dir != null)
                    {
                        tc.WriteLine("static const char *config_dir = \"{0}\";", config_dir);
                    }
                    else
                    {
                        tc.WriteLine("static const char *config_dir = NULL;");
                    }

                    Stream template_stream;
                    if (compress)
                    {
                        template_stream = Assembly.GetAssembly(typeof(MakeBundle)).GetManifestResourceStream("template_z.c");
                    }
                    else
                    {
                        template_stream = Assembly.GetAssembly(typeof(MakeBundle)).GetManifestResourceStream("template.c");
                    }

                    StreamReader s        = new StreamReader(template_stream);
                    string       template = s.ReadToEnd();
                    tc.Write(template);

                    if (!nomain)
                    {
                        Stream       template_main_stream = Assembly.GetAssembly(typeof(MakeBundle)).GetManifestResourceStream("template_main.c");
                        StreamReader st           = new StreamReader(template_main_stream);
                        string       maintemplate = st.ReadToEnd();
                        tc.Write(maintemplate);
                    }

                    tc.Close();

                    if (compile_only)
                    {
                        return;
                    }

                    string zlib      = (compress ? "-lz" : "");
                    string debugging = "-g";
                    string cc        = GetEnv("CC", IsUnix ? "cc" : "gcc -mno-cygwin");

                    if (style == "linux")
                    {
                        debugging = "-ggdb";
                    }
                    if (static_link)
                    {
                        string smonolib;
                        if (style == "osx")
                        {
                            smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
                        }
                        else
                        {
                            smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
                        }
                        cmd = String.Format("{4} -o {2} -Wall `pkg-config --cflags mono-2` {0} {3} " +
                                            "`pkg-config --libs-only-L mono-2` " + smonolib +
                                            "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
                                            temp_c, temp_o, output, zlib, cc);
                    }
                    else
                    {
                        cmd = String.Format("{4} " + debugging + " -o {2} -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
                                            temp_c, temp_o, output, zlib, cc);
                    }

                    ret = Execute(cmd);
                    if (ret != 0)
                    {
                        Error("[Fail]");
                        return;
                    }
                    Console.WriteLine("Done");
                }
            }
        } finally {
            if (!keeptemp)
            {
                if (object_out == null)
                {
                    File.Delete(temp_o);
                }
                if (!compile_only)
                {
                    File.Delete(temp_c);
                }
                File.Delete(temp_s);
            }
        }
    }
예제 #44
0
        public void Setup()
        {
            this._g = new Graph();
            this._g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            this._results = this._g.ExecuteQuery("SELECT * WHERE { ?s ?p ?o }") as SparqlResultSet;

            foreach (MimeTypeDefinition def in MimeTypesHelper.Definitions)
            {
                if (def.CanWriteRdf && def.CanParseRdf)
                {
                    IRdfWriter writer = def.GetRdfWriter();

                    bool   isManual = !def.CanonicalFileExtension.EndsWith(".gz");
                    String filename = "gzip-tests" + (isManual ? String.Empty : "-auto") + "." + def.CanonicalFileExtension + (isManual ? ".gz" : String.Empty);

                    if (isManual)
                    {
                        using (StreamWriter output = new StreamWriter(new GZipStream(new FileStream(filename, FileMode.Create, FileAccess.Write), CompressionMode.Compress)))
                        {
                            writer.Save(this._g, output);
                            output.Close();
                        }

                        this._manualTestFiles.Add(filename);
                    }
                    else
                    {
                        writer.Save(this._g, filename);

                        this._autoTestFiles.Add(filename);
                    }
                }
                else if (def.CanParseRdfDatasets && def.CanWriteRdfDatasets)
                {
                    IStoreWriter writer = def.GetRdfDatasetWriter();

                    bool   isManual = !def.CanonicalFileExtension.EndsWith(".gz");
                    String filename = "gzip-tests-datasets" + (isManual ? String.Empty : "-auto") + "." + def.CanonicalFileExtension + (isManual ? ".gz" : String.Empty);

                    TripleStore store = new TripleStore();
                    store.Add(this._g);

                    if (isManual)
                    {
                        using (Stream output = new GZipStream(new FileStream(filename, FileMode.Create, FileAccess.Write), CompressionMode.Compress))
                        {
                            writer.Save(store, new StreamWriter(output));
                            output.Close();
                        }

                        this._manualDatasetTestFiles.Add(filename);
                    }
                    else
                    {
                        writer.Save(store, new StreamWriter(new FileStream(filename, FileMode.Create, FileAccess.Write)));

                        this._autoDatasetTestFiles.Add(filename);
                    }
                }
                else if (def.CanParseSparqlResults && def.CanWriteSparqlResults)
                {
                    ISparqlResultsWriter writer = def.GetSparqlResultsWriter();

                    bool   isManual = !def.CanonicalFileExtension.EndsWith(".gz");
                    String filename = "gzip-tests-results" + (isManual ? String.Empty : "-auto") + "." + def.CanonicalFileExtension + (isManual ? ".gz" : String.Empty);

                    if (isManual)
                    {
                        using (StreamWriter output = new StreamWriter(new GZipStream(new FileStream(filename, FileMode.Create, FileAccess.Write), CompressionMode.Compress)))
                        {
                            writer.Save(this._results, output);
                            output.Close();
                        }

                        this._manualResultsTestFiles.Add(filename);
                    }
                    else
                    {
                        writer.Save(this._results, new StreamWriter(filename));

                        this._autoResultsTestFiles.Add(filename);
                    }
                }
            }
        }
예제 #45
0
    } // End of the CreateXmlRow method

    /// <summary>
    /// Get the database version from an xml file
    /// </summary>
    /// <returns>The database version as an int</returns>
    public static Int32 GetDatabaseVersion()
    {
        // Create the int to return
        Int32 databaseVersion = 0;

        // Set the filename
        string filename = HttpContext.Current.Server.MapPath("/DatabaseFiles/DatabaseVersion.xml.gz");

        // Check if the file exists
        if (File.Exists(filename) == false)
            return databaseVersion;

        // Create variables
        GZipStream gzipStream = null;
        XmlTextReader xmlTextReader = null;

        try
        {
            // Create a gzip stream
            gzipStream = new GZipStream(new FileStream(filename, FileMode.Open), CompressionMode.Decompress);

            // Create a xml text reader
            xmlTextReader = new XmlTextReader(gzipStream);

            // Read the xml file
            while (xmlTextReader.Read())
            {
                // Get specific information
                if (xmlTextReader.Name == "Version")
                    Int32.TryParse(xmlTextReader.ReadString(), out databaseVersion);
            }

        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            // Close streams
            if (xmlTextReader != null)
            {
                // Close the XmlTextReader
                xmlTextReader.Close();
            }
            if (gzipStream != null)
            {
                // Close the gzip stream
                gzipStream.Close();
            }
        }

        // Return the database version
        return databaseVersion;

    } // End of the GetDatabaseVersion method
예제 #46
0
        private static void EncryptFile(string password, string inputFile, string outputFile, bool compression)
        {
            try
            {
                var sw = new Stopwatch();

                var rng = RNGCryptoServiceProvider.Create();

                var sig           = Encoding.UTF8.GetBytes("GCM");
                var salt          = new byte[SALT_LENGTH];
                var key2          = new byte[KEY_LENGTH];
                var key2Encrypted = new byte[KEY_LENGTH];
                rng.GetBytes(salt);
                rng.GetBytes(key2);

                Rfc2898DeriveBytes k1   = new Rfc2898DeriveBytes(password, salt, 10000, HashAlgorithmName.SHA256);
                byte[]             key1 = k1.GetBytes(32);

                byte[] key2EncryptedTag = new byte[TAG_LENGTH];
                key2Encrypted = GcmEncrypt(key2, key1, FEK_NONCE, key2EncryptedTag);

                using (FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read, BUFFER_SIZE))
                    using (FileStream fsOut = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE))
                    {
                        var compressed = new byte[1] {
                            (byte)(compression ? 1 : 0)
                        };
                        var BEchunkSize  = BigEndianBytesFromInt(CHUNK_SIZE);
                        var versionMajor = new byte[] { VERSION_MAJOR };
                        var versionMinor = new byte[] { VERSION_MINOR };

                        byte[] header;
                        var    headerTag = new byte[TAG_LENGTH];

                        //Build the file header in memory and calculate a tag for it
                        using (MemoryStream ms = new MemoryStream())
                        {
                            ms.Write(sig, 0, sig.Length);                           //3
                            ms.Write(versionMajor, 0, versionMajor.Length);         //1
                            ms.Write(versionMinor, 0, versionMinor.Length);         //1
                            ms.Write(salt, 0, salt.Length);                         //16
                            ms.Write(key2Encrypted, 0, key2Encrypted.Length);       //32
                            ms.Write(key2EncryptedTag, 0, key2EncryptedTag.Length); //16
                            ms.Write(compressed, 0, compressed.Length);             //1
                            ms.Write(BEchunkSize, 0, BEchunkSize.Length);           //4
                            header = ms.ToArray();
                            GcmEncrypt(NO_DATA, key1, HEADER_NONCE, headerTag, header);
                        }

                        fsOut.Write(header, 0, header.Length);
                        fsOut.Write(headerTag, 0, headerTag.Length);

                        //Now get the encryption done.
                        using (var ms = new MemoryStream())
                            using (GZipStream gstr = compression ? new GZipStream(ms, CompressionMode.Compress, true) : null)
                            {
                                sw.Start();
                                if (compression)
                                {
                                    fsIn.CopyTo(gstr);
                                    gstr.Close();                               //Must close!! Flush will not do it!
                                    ms.Position = 0;
                                    ChunkedEncrypt(key2, CHUNK_SIZE, ms, fsOut);
                                }
                                else
                                {
                                    ChunkedEncrypt(key2, CHUNK_SIZE, fsIn, fsOut);
                                }
                            }
                    }
                sw.Stop();
                Console.WriteLine("File encrypted. AES GCM encryption took {0} ms", sw.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Encryption failed: {ex.Message}");
            }
        }
예제 #47
0
 public void Dispose()
 {
     _outputStream.Close();
     GC.SuppressFinalize(this);
 }
예제 #48
0
        private void WriteWorld(string file, int w, int h)
        {
            List <byte> data = new List <byte>(w * h * 4 + 8);

            data.AddRange(BitConverter.GetBytes(w));
            data.AddRange(BitConverter.GetBytes(h));
            Vector3I loc = Vector3I.Zero;

            for (loc.Z = 0; loc.Z < h; loc.Z++)
            {
                for (loc.X = 0; loc.X < w; loc.X++)
                {
                    loc.Y = Region.HEIGHT - 1;
                    VoxLocation vl = new VoxLocation(loc);
                    Region      r = state.World.regions[vl.RegionIndex];
                    ushort      id = 0;
                    int         terr = -1, scen = -1, ramp = -1;
                    // Write Scenery
                    for (; vl.VoxelLoc.Y > 0; vl.VoxelLoc.Y--)
                    {
                        id   = r.voxels[vl.VoxelIndex].ID;
                        terr = id - MINID_TERRAIN;
                        scen = id - MINID_SCENERY;
                        ramp = id - MINID_RAMP;
                        if (terr >= 0 && terr < COUNT_TERRAIN)
                        {
                            break;
                        }
                        else if (ramp >= 0 && ramp < COUNT_RAMP)
                        {
                            break;
                        }
                        else if (scen >= 0 && ramp < COUNT_SCENERY)
                        {
                            data.AddRange(BitConverter.GetBytes(scen));
                            data.AddRange(BitConverter.GetBytes(vl.RegionIndex));
                            data.AddRange(BitConverter.GetBytes(vl.VoxelIndex));
                        }
                    }
                    data.AddRange(BitConverter.GetBytes(-1));

                    // Write Surface
                    data.AddRange(BitConverter.GetBytes(vl.VoxelLoc.Y));
                    if (terr >= 0 && terr < COUNT_TERRAIN)
                    {
                        data.AddRange(BitConverter.GetBytes(0));
                        data.AddRange(BitConverter.GetBytes(terr));
                    }
                    else if (ramp >= 0 && ramp < COUNT_RAMP)
                    {
                        data.AddRange(BitConverter.GetBytes(1));
                        data.AddRange(BitConverter.GetBytes(ramp));
                    }
                    else
                    {
                        data.AddRange(BitConverter.GetBytes(-1));
                    }
                }
            }

            using (MemoryStream ms = new MemoryStream()) {
                var gs = new GZipStream(ms, CompressionMode.Compress, true);
                gs.Write(data.ToArray(), 0, data.Count);
                gs.Close();
                ms.Position = 0;
                using (var s = File.Create(file)) {
                    var bw = new BinaryWriter(s);
                    bw.Write(data.Count);
                    bw.Flush();
                    ms.CopyTo(s);
                    s.Flush();
                }
            }
        }
예제 #49
0
파일: Class5.cs 프로젝트: daviastroaw/newts
 public static byte[] smethod_18(DataSet dataSet_0)
 {
     byte[] result;
     try
     {
         MemoryStream memoryStream = new MemoryStream();
         GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress);
         dataSet_0.WriteXml(gZipStream, XmlWriteMode.WriteSchema);
         gZipStream.Close();
         byte[] array = memoryStream.ToArray();
         memoryStream.Close();
         result = array;
     }
     catch (Exception expr_2E)
     {
         ProjectData.SetProjectError(expr_2E);
         result = null;
         ProjectData.ClearProjectError();
     }
     return result;
 }
예제 #50
0
 /// <summary>
 /// Compacta um array de bytes com Zip
 /// <summary>
 //public static byte[] Zip(byte[] inputByteArray)
 //{
 //    if (inputByteArray == null || inputByteArray.Length == 0)
 //    {
 //        return new byte[0];
 //    }
 //    try
 //    {
 //        using (MemoryStream ms = new MemoryStream())
 //        {
 //            using (ZipOutputStream zipOut = new ZipOutputStream(ms))
 //            {
 //                ZipEntry ZipEntry = new ZipEntry("ZippedFile");
 //                zipOut.PutNextEntry(ZipEntry);
 //                zipOut.SetLevel(9);
 //                zipOut.Write(inputByteArray, 0, inputByteArray.Length);
 //                zipOut.Finish();
 //                zipOut.Close();
 //                return ms.ToArray();
 //            }
 //        }
 //    }
 //    catch
 //    {
 //        return new byte[0];
 //    }
 //}
 /// <summary>
 /// Descompacta um array de bytes com Zip
 /// <summary>
 //public byte[] UnZip(byte[] inputByteArray)
 //{
 //    if (inputByteArray == null || inputByteArray.Length == 0)
 //    {
 //        return new byte[0];
 //    }
 //    try
 //    {
 //        using (MemoryStream ms = new MemoryStream(inputByteArray))
 //        {
 //            using (MemoryStream ret = new MemoryStream())
 //            {
 //                using (ZipInputStream zipIn = new ZipInputStream(ms))
 //                {
 //                    ZipEntry theEntry = zipIn.GetNextEntry();
 //                    byte[] buffer = new byte[2048];
 //                    int size = 2048;
 //                    while (true)
 //                    {
 //                        size = zipIn.Read(buffer, 0, buffer.Length);
 //                        if (size > 0)
 //                        {
 //                            ret.Write(buffer, 0, size);
 //                        }
 //                        else
 //                        {
 //                            break;
 //                        }
 //                    }
 //                    return ret.ToArray();
 //                }
 //            }
 //        }
 //    }
 //    catch
 //    {
 //        return new byte[0];
 //    }
 //}
 /// <summary>
 /// Compacta um array de bytes com GZip
 /// <summary>
 public byte[] Gzip(byte[] data)
 {
     if (data == null || data.Length == 0)
     {
         return new byte[0];
     }
     try
     {
         using (MemoryStream output = new MemoryStream())
         {
             using (GZipStream gzip = new GZipStream(output, CompressionMode.Compress))
             {
                 gzip.Write(data, 0, data.Length);
                 gzip.Close();
                 return output.ToArray();
             }
         }
     }
     catch
     {
         return new byte[0];
     }
 }
예제 #51
0
    /// <summary>
    /// Create the sitemap for a domain
    /// </summary>
    /// <param name="domain">A reference to the domain</param>
    /// <param name="priorityCategories">The priority for categories</param>
    /// <param name="priorityPosts">The priority for posts</param>
    /// <param name="changeFrequency">The change frequency</param>
    public static void CreateSitemap(Domain domain, string priorityCategories, string priorityPosts, string changeFrequency)
    {
        // Create the directory path
        string directoryPath = HttpContext.Current.Server.MapPath("/Content/domains/" + domain.id.ToString() + "/sitemaps/");

        // Check if the directory exists
        if (System.IO.Directory.Exists(directoryPath) == false)
        {
            // Create the directory
            System.IO.Directory.CreateDirectory(directoryPath);
        }

        // Create the file
        string filepath = directoryPath + "Sitemap.xml.gz";

        // Get categories, products and static pages
        List<Category> categories = Category.GetAll(domain.front_end_language, "title", "ASC");
        List<Post> posts = Post.GetAll(domain.front_end_language, "title", "ASC");

        // Create variables
        GZipStream gzipStream = null;
        XmlTextWriter xmlTextWriter = null;

        try
        {
            // Create a gzip stream
            gzipStream = new GZipStream(new FileStream(filepath, FileMode.Create), CompressionMode.Compress);

            // Create a xml text writer
            xmlTextWriter = new XmlTextWriter(gzipStream, new UTF8Encoding(true));

            // Write the start of the document
            xmlTextWriter.WriteStartDocument();

            // Write the url set for the xml document <urlset>
            xmlTextWriter.WriteStartElement("urlset");
            xmlTextWriter.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
            xmlTextWriter.WriteAttributeString("xmlns:image", "http://www.google.com/schemas/sitemap-image/1.1");
            xmlTextWriter.WriteAttributeString("xmlns:video", "http://www.google.com/schemas/sitemap-video/1.1");

            // Create the start string
            string baseUrl = domain.web_address;

            // Add the baseurl
            CreateUrlPost(xmlTextWriter, baseUrl, "1.0", changeFrequency, DateTime.UtcNow);

            // Loop categories
            for (int i = 0; i < categories.Count; i++)
            {
                // Create the url post
                CreateUrlPost(xmlTextWriter, baseUrl + "/home/category/" + categories[i].page_name, priorityCategories, changeFrequency, DateTime.UtcNow);
            }

            // Loop posts
            for (int i = 0; i < posts.Count; i++)
            {
                // Create the url post
                CreateUrlPost(xmlTextWriter, baseUrl + "/home/post/" + posts[i].page_name, priorityPosts, changeFrequency, DateTime.UtcNow);
            }

            // Write the end tag for the xml document </urlset>
            xmlTextWriter.WriteEndDocument();
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            // Close streams
            if (xmlTextWriter != null)
            {
                // Close the XmlTextWriter
                xmlTextWriter.Close();
            }
            if (gzipStream != null)
            {
                // Close the gzip stream
                gzipStream.Close();
            }
        }

    } // End of the CreateSitemap method
    public static void GZipCompressDecompress(string filename)
    {
        Console.WriteLine("Test compression and decompression on file {0}", filename);
        FileStream infile;
        try
        {
            // Open the file as a FileStream object.
            infile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] buffer = new byte[infile.Length];
            // Read the file to ensure it is readable.
            int count = infile.Read(buffer, 0, buffer.Length);
            if (count != buffer.Length)
            {
                infile.Close();
                Console.WriteLine("Test Failed: Unable to read data from file");
                return;
            }
            infile.Close();
            MemoryStream ms = new MemoryStream();
            // Use the newly created memory stream for the compressed data.
            GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
            Console.WriteLine("Compression");
            compressedzipStream.Write(buffer, 0, buffer.Length);
            // Close the stream.
            compressedzipStream.Close();
            Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);

            // Reset the memory stream position to begin decompression.
            ms.Position = 0;
            GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);
            Console.WriteLine("Decompression");
            byte[] decompressedBuffer = new byte[buffer.Length + 100];
            // Use the ReadAllBytesFromStream to read the stream.
            int totalCount = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer);
            Console.WriteLine("Decompressed {0} bytes", totalCount);

            if (!GZipTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount))
            {
                Console.WriteLine("Error. The two buffers did not compare.");
            }
            zipStream.Close();
        } // end try
        catch (InvalidDataException)
        {
            Console.WriteLine("Error: The file being read contains invalid data.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Error:The file specified was not found.");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters");
        }
        catch (PathTooLongException)
        {
            Console.WriteLine("Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.");
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("Error: The specified path is invalid, such as being on an unmapped drive.");
        }
        catch (IOException)
        {
            Console.WriteLine("Error: An I/O error occurred while opening the file.");
        }
        catch (UnauthorizedAccessException)
        {
            Console.WriteLine("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions.");
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("Error: You must provide parameters for MyGZIP.");
        }
    }
예제 #53
0
    public override void FromXML(XmlNode node)
    {
        if (node != null && node.Name == "layer")
        {
            XmlAttributeCollection attrs = node.Attributes;
            m_name = attrs["name"].Value;
            m_layerDimensions.first = Convert.ToInt32(attrs["width"].Value);
            m_layerDimensions.second = Convert.ToInt32(attrs["height"].Value);
            foreach(XmlNode child in node.ChildNodes)
            {
                if (child.Name == "properties")
                {
                    foreach (XmlNode propertyNode in child)
                    {
                        if (propertyNode.Name != "property")
                            continue;
                        XmlAttributeCollection propertyAtts = propertyNode.Attributes;
                        m_properties[propertyAtts["name"].Value] = propertyAtts["value"].Value;
                    }
                }
                else if (child.Name == "data")
                {
                    m_data = new TileData();
                    attrs = child.Attributes;
                    if (attrs["encoding"]!= null)
                    {
                        string[] encodings = { "", "csv", "base64" };
                        string encodingValue = attrs["encoding"].Value;
                        int encodingIdx = Array.IndexOf(encodings, encodingValue);
                        if (encodingIdx >= 0)
                        {
                            m_data.m_encoding = (TileEncodingType)encodingIdx;
                        }

                        string[] compressions = { "", "gzip", "zlib" };
                        string compression = attrs["compression"].Value;
                        int compressionIdx = Array.IndexOf(compressions, compression);
                        if (compressionIdx >= 0)
                        {
                            m_data.m_compression = (TileCompressionType)compressionIdx;
                        }

                        switch(m_data.m_encoding)
                        {
                            case TileEncodingType.kCSV:
                                {
                                    string text = child.InnerText;
                                    string[] values = text.Split(',');
                                    foreach (string v in values)
                                    {
                                        uint value = Convert.ToUInt32(v);
                                        TileInfo info = new TileInfo();
                                        info.m_horizontalFlip = (value & FLIPPED_HORIZONTALLY_FLAG) != 0;
                                        info.m_verticalFlip = (value & FLIPPED_VERTICALLY_FLAG) != 0;
                                        info.m_diagonalFlip = (value & FLIPPED_DIAGONALLY_FLAG) != 0;
                                        value = value & ~(FLIPPED_DIAGONALLY_FLAG | FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG);
                                        info.m_gid = value;
                                        m_data.m_tiles.Add(info);
                                    }
                                    break;
                                }
                            case TileEncodingType.kBase64:
                                {
                                    byte[] bytes = null;
                                    switch(m_data.m_compression)
                                    {
                                        case TileCompressionType.kNone:
                                            {
                                                bytes = Convert.FromBase64String(child.InnerText);
                                                break;
                                            }
                                        case TileCompressionType.kGzip:
                                            {
                                                //Transform string into byte[]
                                                string str = child.InnerText;
                                                byte[] byteArray = new byte[str.Length];
                                                int indexBA = 0;
                                                foreach (char item in str.ToCharArray())
                                                {
                                                    byteArray[indexBA++] = (byte)item;
                                                }

                                                MemoryStream ms = new MemoryStream(byteArray);
                                                GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);

                                                byteArray = new byte[byteArray.Length];
                                                int rBytes = gzip.Read(byteArray, 0, byteArray.Length);

                                                StringBuilder sb = new StringBuilder(rBytes);
                                                for (int i = 0; i < rBytes; ++i)
                                                {
                                                    sb.Append((char)byteArray[i]);
                                                }

                                                gzip.Close();
                                                ms.Close();
                                                gzip.Dispose();
                                                ms.Dispose();

                                                bytes = Convert.FromBase64String(sb.ToString());
                                                break;
                                            }
                                        case TileCompressionType.kZlib:
                                            {
                                                //Transform string into byte[]
                                                string str = child.InnerText;
                                                byte[] byteArray = new byte[str.Length];
                                                int indexBA = 0;
                                                foreach (char item in str.ToCharArray())
                                                {
                                                    byteArray[indexBA++] = (byte)item;
                                                }

                                                MemoryStream ms = new MemoryStream(byteArray);
                                                DeflateStream zlib = new DeflateStream(ms, CompressionMode.Decompress);

                                                byteArray = new byte[byteArray.Length];
                                                int rBytes = zlib.Read(byteArray, 0, byteArray.Length);

                                                StringBuilder sb = new StringBuilder(rBytes);
                                                for (int i = 0; i < rBytes; ++i)
                                                {
                                                    sb.Append((char)byteArray[i]);
                                                }

                                                zlib.Close();
                                                ms.Close();
                                                zlib.Dispose();
                                                ms.Dispose();

                                                bytes = Convert.FromBase64String(sb.ToString());
                                                break;
                                            }
                                    }
                                    for (int i = 0; i < bytes.Length; i += 4)
                                    {
                                        uint value = (uint)bytes[i] | ((uint)bytes[i + 1] << 8) | ((uint)bytes[i + 2] << 16) | ((uint)bytes[i + 3] << 24);
                                        TileInfo info = new TileInfo();
                                        info.m_horizontalFlip = (value & FLIPPED_HORIZONTALLY_FLAG) != 0;
                                        info.m_verticalFlip = (value & FLIPPED_VERTICALLY_FLAG) != 0;
                                        info.m_diagonalFlip = (value & FLIPPED_DIAGONALLY_FLAG) != 0;
                                        value = value & ~(FLIPPED_DIAGONALLY_FLAG | FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG);
                                        info.m_gid = value;
                                        m_data.m_tiles.Add(info);
                                    }
                                    break;
                                }
                            default:
                                {
                                    break;
                                }
                        }
                    }
                    else
                    {
                        m_data.m_encoding = TileEncodingType.kNone;
                        m_data.m_compression = TileCompressionType.kNone;

                        m_data.m_tiles.Clear();
                        foreach(XmlNode tileNode in child.ChildNodes)
                        {
                            if (tileNode.Name != "tile")
                            {
                                continue;
                            }
                            TileInfo info = new TileInfo();
                            uint value = Convert.ToUInt32(tileNode.Attributes["gid"].Value);

                            info.m_horizontalFlip = (value & FLIPPED_HORIZONTALLY_FLAG) != 0;
                            info.m_verticalFlip = (value & FLIPPED_VERTICALLY_FLAG) != 0;
                            info.m_diagonalFlip = (value & FLIPPED_DIAGONALLY_FLAG) != 0;
                            value = value & ~(FLIPPED_DIAGONALLY_FLAG | FLIPPED_HORIZONTALLY_FLAG |FLIPPED_VERTICALLY_FLAG);
                            info.m_gid = value;
                            m_data.m_tiles.Add(info);
                        }
                    }
                }
            }
        }
    }
예제 #54
0
        /// <summary>
        /// Get compress stream from file. Use DeflateStream.
        /// </summary>
        /// <param name="fileName">File name</param>
        /// <param name="compressMethod">Compression method</param>
        /// <returns>Compress stream.</returns>
        public static byte[] GetCompressStreamFromFile(
            this string fileName,
            CompressMethod compressMethod = CompressMethod.Deflate)
        {
            var        tmpFile = Path.GetTempFileName();
            FileStream fs      = null;
            Stream     zip     = null;

            try
            {
                switch (compressMethod)
                {
                case CompressMethod.Deflate:
                    zip = new DeflateStream(new FileStream(tmpFile, FileMode.Create, FileAccess.Write),
                                            CompressionMode.Compress);
                    break;

                case CompressMethod.GZip:
                    zip = new GZipStream(new FileStream(tmpFile, FileMode.Create, FileAccess.Write),
                                         CompressionMode.Compress);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(compressMethod));
                }

                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                fs.CopyTo(zip);
            }
            catch (Exception)
            {
                if (File.Exists(tmpFile))
                {
                    File.Delete(tmpFile);
                }
                throw;
            }
            finally
            {
                zip?.Flush();
                zip?.Close();
                zip?.Dispose();
                fs?.Close();
                fs?.Dispose();
            }

            byte[] res = null;
            try
            {
                if (File.Exists(tmpFile))
                {
                    res = GetStreamFromFile(tmpFile);
                }
            }
            finally
            {
                if (File.Exists(tmpFile))
                {
                    File.Delete(tmpFile);
                }
            }

            return(res);
        }
예제 #55
0
    /// <summary>
    /// GZip encodes a memory buffer to a compressed memory buffer
    /// </summary>
    /// <param name="Buffer"></param>
    /// <returns></returns>
    public static byte[] GZipMemory(byte[] Buffer)
    {
        MemoryStream ms = new MemoryStream();

        GZipStream GZip = new GZipStream(ms, CompressionMode.Compress);

        GZip.Write(Buffer, 0, Buffer.Length);
        GZip.Close();

        byte[] Result = ms.ToArray();
        ms.Close();

        return Result;
    }
예제 #56
0
파일: MemUtil.cs 프로젝트: elitak/keepass
        public static byte[] Compress(byte[] pbData)
        {
            if(pbData == null) throw new ArgumentNullException("pbData");
            if(pbData.Length == 0) return pbData;

            MemoryStream msCompressed = new MemoryStream();
            GZipStream gz = new GZipStream(msCompressed, CompressionMode.Compress);
            MemoryStream msSource = new MemoryStream(pbData, false);
            MemUtil.CopyStream(msSource, gz);
            gz.Close();
            msSource.Close();

            byte[] pbCompressed = msCompressed.ToArray();
            msCompressed.Close();
            return pbCompressed;
        }
예제 #57
0
파일: mkbundle.cs 프로젝트: mono/mono
	static void GenerateBundles (List<string> files)
	{
		string temp_s = "temp.s"; // Path.GetTempFileName ();
		string temp_c = "temp.c";
		string temp_o = "temp.o";

		if (compile_only)
			temp_c = output;
		if (object_out != null)
			temp_o = object_out;
		
		try {
			List<string> c_bundle_names = new List<string> ();
			List<string[]> config_names = new List<string[]> ();

			using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
			using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
			string prog = null;

#if XAMARIN_ANDROID
			tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
			tc.WriteLine ("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
			tc.WriteLine (@"
typedef struct {
	const char *name;
	const unsigned char *data;
	const unsigned int size;
} MonoBundledAssembly;
void          mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
void          mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
");
#else
			tc.WriteLine ("#include <mono/metadata/mono-config.h>");
			tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
#endif

			if (compress) {
				tc.WriteLine ("typedef struct _compressed_data {");
				tc.WriteLine ("\tMonoBundledAssembly assembly;");
				tc.WriteLine ("\tint compressed_size;");
				tc.WriteLine ("} CompressedAssembly;\n");
			}

			object monitor = new object ();

			var streams = new Dictionary<string, Stream> ();
			var sizes = new Dictionary<string, long> ();

			// Do the file reading and compression in parallel
			Action<string> body = delegate (string url) {
				string fname = LocateFile (new Uri (url).LocalPath);
				Stream stream = File.OpenRead (fname);

				long real_size = stream.Length;
				int n;
				if (compress) {
					byte[] cbuffer = new byte [8192];
					MemoryStream ms = new MemoryStream ();
					GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
					while ((n = stream.Read (cbuffer, 0, cbuffer.Length)) != 0){
						deflate.Write (cbuffer, 0, n);
					}
					stream.Close ();
					deflate.Close ();
					byte [] bytes = ms.GetBuffer ();
					stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
				}

				lock (monitor) {
					streams [url] = stream;
					sizes [url] = real_size;
				}
			};

			//#if NET_4_5
#if FALSE
			Parallel.ForEach (files, body);
#else
			foreach (var url in files)
				body (url);
#endif

			// The non-parallel part
			byte [] buffer = new byte [8192];
			// everything other than a-zA-Z0-9_ needs to be escaped in asm symbols.
			var symbolEscapeRE = new System.Text.RegularExpressions.Regex ("[^\\w_]");
			foreach (var url in files) {
				string fname = LocateFile (new Uri (url).LocalPath);
				string aname = MakeBundle.GetAssemblyName (fname);
				string encoded = symbolEscapeRE.Replace (aname, "_");

				if (prog == null)
					prog = aname;

				var stream = streams [url];
				var real_size = sizes [url];

				if (!quiet)
					Console.WriteLine ("   embedding: " + fname);

				WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
			
				WriteBuffer (ts, stream, buffer);

				if (compress) {
					tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
					tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
								  " assembly_data_{0}, {2}}}, {3}}};",
								  encoded, aname, real_size, stream.Length);
					if (!quiet) {
						double ratio = ((double) stream.Length * 100) / real_size;
						Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
					}
				} else {
					tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
					tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
								  encoded, aname, real_size);
				}
				stream.Close ();

				c_bundle_names.Add ("assembly_bundle_" + encoded);

				try {
					FileStream cf = File.OpenRead (fname + ".config");
					if (!quiet)
						Console.WriteLine (" config from: " + fname + ".config");
					tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
					WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
					WriteBuffer (ts, cf, buffer);
					ts.WriteLine ();
					config_names.Add (new string[] {aname, encoded});
				} catch (FileNotFoundException) {
					/* we ignore if the config file doesn't exist */
				}
			}

			if (config_file != null){
				FileStream conf;
				try {
					conf = File.OpenRead (config_file);
				} catch {
					Error ("Failure to open {0}", config_file);
					return;
				}
				if (!quiet)
					Console.WriteLine ("System config from: " + config_file);
				tc.WriteLine ("extern const char system_config;");
				WriteSymbol (ts, "system_config", config_file.Length);

				WriteBuffer (ts, conf, buffer);
				// null terminator
				ts.Write ("\t.byte 0\n");
				ts.WriteLine ();
			}

			if (machine_config_file != null){
				FileStream conf;
				try {
					conf = File.OpenRead (machine_config_file);
				} catch {
					Error ("Failure to open {0}", machine_config_file);
					return;
				}
				if (!quiet)
					Console.WriteLine ("Machine config from: " + machine_config_file);
				tc.WriteLine ("extern const char machine_config;");
				WriteSymbol (ts, "machine_config", machine_config_file.Length);

				WriteBuffer (ts, conf, buffer);
				ts.Write ("\t.byte 0\n");
				ts.WriteLine ();
			}
			ts.Close ();

			if (compress)
				tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
			else
				tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");

			foreach (string c in c_bundle_names){
				tc.WriteLine ("\t&{0},", c);
			}
			tc.WriteLine ("\tNULL\n};\n");
			tc.WriteLine ("static char *image_name = \"{0}\";", prog);

			if (ctor_func != null) {
				tc.WriteLine ("\nextern void {0} (void);", ctor_func);
				tc.WriteLine ("\n__attribute__ ((constructor)) static void mono_mkbundle_ctor (void)");
				tc.WriteLine ("{{\n\t{0} ();\n}}", ctor_func);
			}

			tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
			foreach (string[] ass in config_names){
				tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
			}
			if (config_file != null)
				tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
			if (machine_config_file != null)
				tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
			tc.WriteLine ("}\n");

			if (config_dir != null)
				tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
			else
				tc.WriteLine ("static const char *config_dir = NULL;");

			Stream template_stream;
			if (compress) {
				template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
			} else {
				template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
			}

			StreamReader s = new StreamReader (template_stream);
			string template = s.ReadToEnd ();
			tc.Write (template);

			if (!nomain && custom_main == null) {
				Stream template_main_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
				StreamReader st = new StreamReader (template_main_stream);
				string maintemplate = st.ReadToEnd ();
				tc.Write (maintemplate);
			}

			tc.Close ();

			string assembler = GetEnv("AS", "as");
			string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
			Execute(as_cmd);

			if (compile_only)
				return;

			if (!quiet)
				Console.WriteLine("Compiling:");

			if (style == "windows")
			{

				Func<string, string> quote = (pp) => { return "\"" + pp + "\""; };

				string compiler = GetEnv("CC", "cl.exe");
				string winsdkPath = GetEnv("WINSDK", @"C:\Program Files (x86)\Windows Kits\8.1");
				string vsPath = GetEnv("VSINCLUDE", @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC");
				string monoPath = GetEnv("MONOPREFIX", @"C:\Program Files (x86)\Mono");

				string[] includes = new string[] {winsdkPath + @"\Include\um", winsdkPath + @"\Include\shared", vsPath + @"\include", monoPath + @"\include\mono-2.0", "." };
				// string[] libs = new string[] { winsdkPath + @"\Lib\winv6.3\um\x86" , vsPath + @"\lib" };
				var linkLibraries = new string[] {  "kernel32.lib",
												"version.lib",
												"Ws2_32.lib",
												"Mswsock.lib",
												"Psapi.lib",
												"shell32.lib",
												"OleAut32.lib",
												"ole32.lib",
												"winmm.lib",
												"user32.lib",
												"libvcruntime.lib",
												"advapi32.lib",
												"OLDNAMES.lib",
												"libucrt.lib" };

				string glue_obj = "mkbundle_glue.obj";
				string monoLib;

				if (static_link)
					monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0-static.lib");

				else {
					Console.WriteLine ("WARNING: Dynamically linking the Mono runtime on Windows is not a tested option.");
					monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0.lib");
					LocateFile (monoPath + @"\lib\monosgen-2.0.dll"); // in this case, the .lib is just the import library, and the .dll is also needed
				}

				var compilerArgs = new List<string>();
				compilerArgs.Add("/MT");

				foreach (string include in includes)
					compilerArgs.Add(String.Format ("/I {0}", quote (include)));

				if (!nomain || custom_main != null) {
					compilerArgs.Add(quote(temp_c));
					compilerArgs.Add(quote(temp_o));
					if (custom_main != null)
						compilerArgs.Add(quote(custom_main));
					compilerArgs.Add(quote(monoLib));
					compilerArgs.Add("/link");
					compilerArgs.Add("/NODEFAULTLIB");
					compilerArgs.Add("/SUBSYSTEM:windows");
					compilerArgs.Add("/ENTRY:mainCRTStartup");
					compilerArgs.AddRange(linkLibraries);
					compilerArgs.Add("/out:"+ output);

					string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
					Execute (cl_cmd);
				}
				else
				{
					// we are just creating a .lib
					compilerArgs.Add("/c"); // compile only
					compilerArgs.Add(temp_c);
					compilerArgs.Add(String.Format("/Fo" + glue_obj)); // .obj output name

					string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
					Execute (cl_cmd);

					string librarian = GetEnv ("LIB", "lib.exe");
					var librarianArgs = new List<string> ();
					librarianArgs.Add (String.Format ("/out:{0}.lib" + output));
					librarianArgs.Add (temp_o);
					librarianArgs.Add (glue_obj);
					librarianArgs.Add (monoLib);
					string lib_cmd = String.Format("{0} {1}", librarian, String.Join(" ", librarianArgs.ToArray()));
					Execute (lib_cmd);
				}
			}
			else
			{
				string zlib = (compress ? "-lz" : "");
				string debugging = "-g";
				string cc = GetEnv("CC", "cc");
				string cmd = null;

				if (style == "linux")
					debugging = "-ggdb";
				if (static_link)
				{
					string smonolib;
					if (style == "osx")
						smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
					else
						smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
					cmd = String.Format("{4} -o '{2}' -Wall `pkg-config --cflags mono-2` {0} {3} " +
						"`pkg-config --libs-only-L mono-2` " + smonolib +
						"`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
						temp_c, temp_o, output, zlib, cc);
				}
				else
				{

					cmd = String.Format("{4} " + debugging + " -o '{2}' -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
						temp_c, temp_o, output, zlib, cc);
				}
				Execute (cmd);
			}

			if (!quiet)
				Console.WriteLine ("Done");
		}
	}
		} finally {
			if (!keeptemp){
				if (object_out == null){
					File.Delete (temp_o);
				}
				if (!compile_only){
					File.Delete (temp_c);
				}
				File.Delete (temp_s);
			}
		}
	}
예제 #58
0
파일: Parser.cs 프로젝트: xulei-ssh/LC-List
        public static bool SaveList(string proj, List <string> itemsDone, List <Item> items, Dictionary <int, string> stdSuffix, List <ObservableCollection <ListItem> > lists, List <string> lots, string fileName)
        {
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    StreamWriter writer = new StreamWriter(stream);
                    string       s      = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ListDocument>";
                    s += "<Proj name=\"" + proj + "\"/><ItemList>";
                    foreach (var i in itemsDone)
                    {
                        s += "<Item name=\"" + i + "\"/>";
                    }
                    s += "</ItemList><STD>";
                    Dictionary <int, string> stdType = new Dictionary <int, string>();      //STD号,项目名
                    foreach (var i in items)
                    {
                        if (itemsDone.Contains(i.Name) && i.StdType != 0 && !stdType.ContainsKey(i.StdType))
                        {
                            stdType.Add(i.StdType, i.Name);
                        }
                    }
                    if (stdSuffix.Count != 0)
                    {
                        foreach (var i in stdSuffix)
                        {
                            s += "<Entry suf=\"" + stdSuffix[i.Key] + "\" item=\"" + stdType[i.Key] + "\"/>";
                        }
                    }
                    s += "</STD><List>";
                    foreach (var i in lists)
                    {
                        foreach (var j in i)
                        {
                            //设计溶出、含量均匀度、耐酸力的格式,仅第一针
                            string dissolutionIgnoredPattern       = @"^*\-R[2-6]$";                        //所有 溶出的 非 R1结尾的
                            string contentUniformityIgnoredPattern = @"^*\-HJ([0-9]{0,1}[02-9]$)";          //所有 含量均匀度的 非 1\21\31...结尾的
                            string acidIgnoredPattern = @"^*\-N[2-6]$";                                     //所有 耐酸力的 非 R1结尾的
                            if (!(Regex.IsMatch(j.Name, dissolutionIgnoredPattern) || Regex.IsMatch(j.Name, contentUniformityIgnoredPattern) || Regex.IsMatch(j.Name, acidIgnoredPattern)))
                            {
                                s += "<Entry count=\"" + j.Count + "\" name=\"" + j.Name + "\"/>";
                            }
                        }
                    }
                    s += "</List><Lots>";
                    foreach (var i in lots)
                    {
                        if (i != "")
                        {
                            s += "<Lot l=\"" + i + "\"/>";
                        }
                    }
                    s += "</Lots></ListDocument>";
                    s  = s.Replace("&", "[and_placeholder]");

                    writer.Write(s);
                    writer.Flush();
                    stream.Position = 0;
                    string tempFileLocation = AppDomain.CurrentDomain.BaseDirectory + @"Sequences\" + fileName + ".elw.temp";
                    byte[] arr1             = new byte[(int)stream.Length];
                    stream.Read(arr1, 0, (int)stream.Length);
                    for (int i = 0; i < arr1.Length; i++)
                    {
                        arr1[i] = (byte)(arr1[i] - i);
                    }
                    FileStream ms1 = File.Create(tempFileLocation);
                    GZipStream gz  = new GZipStream(ms1, CompressionMode.Compress);
                    gz.Write(arr1, 0, arr1.Length);
                    gz.Close();
                    ms1.Close();
                    File.Move(tempFileLocation, AppDomain.CurrentDomain.BaseDirectory + @"Sequences\" + fileName + ".elw");
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
예제 #59
-1
 /// <summary>
 /// Descompacta um array de bytes com Gzip
 /// <summary>
 public byte[] GUnzip(byte[] data)
 {
     if (data == null || data.Length == 0)
     {
         return new byte[0];
     }
     try
     {
         using (MemoryStream input = new MemoryStream())
         {
             input.Write(data, 0, data.Length);
             input.Position = 0;
             using (GZipStream gzip = new GZipStream(input, CompressionMode.Decompress))
             {
                 using (MemoryStream output = new MemoryStream())
                 {
                     byte[] buff = new byte[64];
                     int read = -1;
                     read = gzip.Read(buff, 0, buff.Length);
                     while (read > 0)
                     {
                         output.Write(buff, 0, read);
                         read = gzip.Read(buff, 0, buff.Length);
                     }
                     gzip.Close();
                     return output.ToArray();
                 }
             }
         }
     }
     catch
     {
         return new byte[0];
     }
 }
예제 #60
-1
파일: mkbundle.cs 프로젝트: skolima/mono
	static void GenerateBundles (List<string> files)
	{
		string temp_s = "temp.s"; // Path.GetTempFileName ();
		string temp_c = "temp.c";
		string temp_o = "temp.o";

		if (compile_only)
			temp_c = output;
		if (object_out != null)
			temp_o = object_out;
		
		try {
			List<string> c_bundle_names = new List<string> ();
			List<string[]> config_names = new List<string[]> ();
			byte [] buffer = new byte [8192];

			using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
			using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
			string prog = null;

#if XAMARIN_ANDROID
			tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
			tc.WriteLine ("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
			tc.WriteLine (@"
typedef struct {
	const char *name;
	const unsigned char *data;
	const unsigned int size;
} MonoBundledAssembly;
void          mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
void          mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
");
#else
			tc.WriteLine ("#include <mono/metadata/mono-config.h>");
			tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
#endif

			if (compress) {
				tc.WriteLine ("typedef struct _compressed_data {");
				tc.WriteLine ("\tMonoBundledAssembly assembly;");
				tc.WriteLine ("\tint compressed_size;");
				tc.WriteLine ("} CompressedAssembly;\n");
			}

			object monitor = new object ();

			var streams = new Dictionary<string, Stream> ();
			var sizes = new Dictionary<string, long> ();

			// Do the file reading and compression in parallel
			Action<string> body = delegate (string url) {
				string fname = new Uri (url).LocalPath;
				Stream stream = File.OpenRead (fname);

				long real_size = stream.Length;
				int n;
				if (compress) {
					MemoryStream ms = new MemoryStream ();
					GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
					while ((n = stream.Read (buffer, 0, buffer.Length)) != 0){
						deflate.Write (buffer, 0, n);
					}
					stream.Close ();
					deflate.Close ();
					byte [] bytes = ms.GetBuffer ();
					stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
				}

				lock (monitor) {
					streams [url] = stream;
					sizes [url] = real_size;
				}
			};

			//#if NET_4_5
#if FALSE
			Parallel.ForEach (files, body);
#else
			foreach (var url in files)
				body (url);
#endif

			// The non-parallel part
			foreach (var url in files) {
				string fname = new Uri (url).LocalPath;
				string aname = Path.GetFileName (fname);
				string encoded = aname.Replace ("-", "_").Replace (".", "_");

				if (prog == null)
					prog = aname;

				var stream = streams [url];
				var real_size = sizes [url];

				Console.WriteLine ("   embedding: " + fname);

				WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
			
				WriteBuffer (ts, stream, buffer);

				if (compress) {
					tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
					tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
								  " assembly_data_{0}, {2}}}, {3}}};",
								  encoded, aname, real_size, stream.Length);
					double ratio = ((double) stream.Length * 100) / real_size;
					Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
				} else {
					tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
					tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
								  encoded, aname, real_size);
				}
				stream.Close ();

				c_bundle_names.Add ("assembly_bundle_" + encoded);

				try {
					FileStream cf = File.OpenRead (fname + ".config");
					Console.WriteLine (" config from: " + fname + ".config");
					tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
					WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
					WriteBuffer (ts, cf, buffer);
					ts.WriteLine ();
					config_names.Add (new string[] {aname, encoded});
				} catch (FileNotFoundException) {
					/* we ignore if the config file doesn't exist */
				}
			}

			if (config_file != null){
				FileStream conf;
				try {
					conf = File.OpenRead (config_file);
				} catch {
					Error (String.Format ("Failure to open {0}", config_file));
					return;
				}
				Console.WriteLine ("System config from: " + config_file);
				tc.WriteLine ("extern const char system_config;");
				WriteSymbol (ts, "system_config", config_file.Length);

				WriteBuffer (ts, conf, buffer);
				// null terminator
				ts.Write ("\t.byte 0\n");
				ts.WriteLine ();
			}

			if (machine_config_file != null){
				FileStream conf;
				try {
					conf = File.OpenRead (machine_config_file);
				} catch {
					Error (String.Format ("Failure to open {0}", machine_config_file));
					return;
				}
				Console.WriteLine ("Machine config from: " + machine_config_file);
				tc.WriteLine ("extern const char machine_config;");
				WriteSymbol (ts, "machine_config", machine_config_file.Length);

				WriteBuffer (ts, conf, buffer);
				ts.Write ("\t.byte 0\n");
				ts.WriteLine ();
			}
			ts.Close ();
			
			Console.WriteLine ("Compiling:");
			string cmd = String.Format ("{0} -o {1} {2} ", GetEnv ("AS", "as"), temp_o, temp_s);
			int ret = Execute (cmd);
			if (ret != 0){
				Error ("[Fail]");
				return;
			}

			if (compress)
				tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
			else
				tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");

			foreach (string c in c_bundle_names){
				tc.WriteLine ("\t&{0},", c);
			}
			tc.WriteLine ("\tNULL\n};\n");
			tc.WriteLine ("static char *image_name = \"{0}\";", prog);

			tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
			foreach (string[] ass in config_names){
				tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
			}
			if (config_file != null)
				tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
			if (machine_config_file != null)
				tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
			tc.WriteLine ("}\n");

			if (config_dir != null)
				tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
			else
				tc.WriteLine ("static const char *config_dir = NULL;");

			Stream template_stream;
			if (compress) {
				template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
			} else {
				template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
			}

			StreamReader s = new StreamReader (template_stream);
			string template = s.ReadToEnd ();
			tc.Write (template);

			if (!nomain) {
				Stream template_main_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
				StreamReader st = new StreamReader (template_main_stream);
				string maintemplate = st.ReadToEnd ();
				tc.Write (maintemplate);
			}
			
			tc.Close ();

			if (compile_only)
				return;

			string zlib = (compress ? "-lz" : "");
			string debugging = "-g";
			string cc = GetEnv ("CC", IsUnix ? "cc" : "i686-pc-mingw32-gcc");

			if (style == "linux")
				debugging = "-ggdb";
			if (static_link) {
				string smonolib;
				if (style == "osx")
					smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
				else
					smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
				cmd = String.Format ("{4} -o {2} -Wall `pkg-config --cflags mono-2` {0} {3} " +
						     "`pkg-config --libs-only-L mono-2` " + smonolib +
						     "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
						     temp_c, temp_o, output, zlib, cc);
			} else {
				
				cmd = String.Format ("{4} " + debugging + " -o {2} -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
						     temp_c, temp_o, output, zlib, cc);
			}
                            
			ret = Execute (cmd);
			if (ret != 0){
				Error ("[Fail]");
				return;
			}
			Console.WriteLine ("Done");
			}
			}
		} finally {
			if (!keeptemp){
				if (object_out == null){
					File.Delete (temp_o);
				}
				if (!compile_only){
					File.Delete (temp_c);
				}
				File.Delete (temp_s);
			}
		}
	}