Ejemplo n.º 1
0
            public static void Decompress(FileInfo archFile, out String szOutFile)
            {
                Logger.Enter();
                using (FileStream archFileStream = archFile.OpenRead())
                {
                    String currentFileName = archFile.FullName;
                    String newFileName = currentFileName.Remove(
                        currentFileName.Length - archFile.Extension.Length);

                    using (FileStream normalFileStream = File.Create(newFileName))
                    {
                        using (GZipStream decompressionStream = new GZipStream(archFileStream, CompressionMode.Decompress))
                        {
                            byte[] buffer = new byte[1024];
                            int nRead;
                            while ((nRead = decompressionStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                normalFileStream.Write(buffer, 0, nRead);
                            }

                            szOutFile = newFileName;
                            Console.WriteLine("Decompressed: {0}", archFile.Name);
                        }
                    }
                }
                Logger.Leave();
            }
Ejemplo n.º 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());
    }
Ejemplo n.º 3
0
    private static void Assemble(List<string> file, string name, string destinationDir)
    {
        Directory.CreateDirectory(destinationDir);
        FileStream write = new FileStream(destinationDir + name, FileMode.Append);

        byte[] buffer = new byte[4096];
        using (write)
        {
            for (int i = 0; i < file.Count; i++)
            {
                using (FileStream reader = new FileStream("../dir/" + file[i], FileMode.Open))
                {
                    using (GZipStream gz = new GZipStream(reader, CompressionMode.Decompress, false))
                    {
                        while (true)
                        {
                            int bytes = gz.Read(buffer, 0, buffer.Length);
                            if (bytes == 0)
                            {
                                break;
                            }
                            write.Write(buffer, 0, bytes);
                        }
                    }
                }
            }
        }
    }
    static void Assemble(List<string> files, string destinationDirectory)
    {
        using (FileStream combined = new FileStream(destinationDirectory + "combined files" + extensionSource, FileMode.Create))
        {
            for (int file = 0; file < files.Count; file++)
            {
                using (FileStream input = new FileStream(files[file], FileMode.Open))
                {
                    using (GZipStream commpression = new GZipStream(input, CompressionMode.Decompress, false))
                    {
                        byte[] buffer = new byte[4096];

                        while (true)
                        {
                            int readBytes = commpression.Read(buffer, 0, buffer.Length);

                            if (readBytes == 0)
                            {
                                break;
                            }
                            combined.Write(buffer, 0, readBytes);
                        }
                    }
                }
            }
        }
    }
 private static void Assemble(List<string> files, string destinationDirectory)
 {
     var allData = new List<byte>();
         for (int i = 0; i < files.Count; i++)
         {
             var sourceFile = files[i];
             using (var source = new FileStream(sourceFile, FileMode.Open))
             {
                 using (var zip = new GZipStream(source, CompressionMode.Decompress))
                 {
                     byte[] buffer = new byte[4096];
                     while (true)
                     {
                         int readBytes = zip.Read(buffer, 0, buffer.Length);
                         if (readBytes == 0)
                         {
                             break;
                         }
                         for (int j = 0; j < readBytes; j++)
                         {
                             allData.Add(buffer[j]);
                         }
                     }
                 }
             }
         }
         using (var copy = new FileStream(destinationDirectory, FileMode.Create))
         {
             copy.Write(allData.ToArray(), 0, allData.Count);
         }
 }
    static void Assemble(string[] files, string destinationDirectory)
    {
        using (var assemly = new FileStream(destinationDirectory + GetFileName() + "-Assembled" + GetExtention(), FileMode.Create))
        {
            for (int i = 0; i < files.Length; i++)
            {
                using (var source = new FileStream(filePartsNames[i] + ".gz", FileMode.Open))
                {
                    using (var gz = new GZipStream(source, CompressionMode.Decompress))
                    {
                        byte[] buffer = new byte[4096];

                        while (true)
                        {
                            int readBytes = gz.Read(buffer, 0, buffer.Length);
                            if (readBytes == 0)
                            {
                                break;
                            }
                            assemly.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 7
0
    private static void Assemble(int parts)
    {
        byte[] buffer = new byte[4096];
        for (int i = 1; i <= parts; i++)
        {
            string source = String.Format("../../{0}.gz", i);
            FileStream partOfFile = new FileStream(source, FileMode.Open);
            FileStream assembledFile = new FileStream("../../assembled.txt", FileMode.Append);

            using (partOfFile)
            {
                using (assembledFile)
                {
                    using (GZipStream decompression = new GZipStream(partOfFile, CompressionMode.Decompress))
                    {
                        while (true)
                        {
                            int bytesRead = decompression.Read(buffer, 0, buffer.Length);
                            if (bytesRead == 0)
                            {
                                break;
                            }
                            assembledFile.Write(buffer, 0, bytesRead);
                        }
                    }

                }
            }
        }
    }
    private static void AssembleGZip(List<string> files, string destinationDirectory)
    {
        string[] dotItems = files[0].Split('.');
        string ext = dotItems[dotItems.Length - 2];

        string destinationFile = destinationDirectory + "assembled." + ext;
        using (FileStream dest = new FileStream(destinationFile, FileMode.Append, FileAccess.Write))
        {

            foreach (string inFile in files)
            {
                using (FileStream source = new FileStream(inFile, FileMode.Open))
                {
                    using (GZipStream sourceGZip = new GZipStream(source, CompressionMode.Decompress, false))
                    {
                        byte[] buffer = new byte[4096];
                        int len;
                        while ((len = sourceGZip.Read(buffer, 0, buffer.Length)) > 0)
                            dest.Write(buffer, 0, len);
                    }

                }
            }
        }
    }
Ejemplo n.º 9
0
    static void Assemble(List<string> files, string destinationDirectory)
    {
        string fileOutputPath = destinationDirectory + "assembled" + "." + matches[0].Groups[3];
        var fsSource = new FileStream(fileOutputPath, FileMode.Create);
        fsSource.Close();

        using (fsSource = new FileStream(fileOutputPath, FileMode.Append))
        {
            // reading the file paths of the parts from the files list
            foreach (var filePart in files)
            {
                using (var partSource = new FileStream(filePart, FileMode.Open))
                {
                    using (var compressionStream = new GZipStream(partSource,CompressionMode.Decompress,false))
                    {
                        // copy the bytes from part to new assembled file
                        Byte[] bytePart = new byte[4096];
                        while (true)
                        {
                            int readBytes = compressionStream.Read(bytePart, 0, bytePart.Length);
                            if (readBytes == 0)
                            {
                                break;
                            }

                            fsSource.Write(bytePart, 0, readBytes);
                        }
                    }
                }
            }
        }
    }
 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();
 }
Ejemplo n.º 11
0
    /// <summary>
    /// 
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] Decompress(byte[] data)
    {
        using (var compressedStream = new MemoryStream(data))
        using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
        using (var resultStream = new MemoryStream())
        {
            var buffer = new byte[4096];
            int read;

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

            return resultStream.ToArray();
        }
    }
Ejemplo n.º 12
0
        public static Page Load(Stream stream)
        {
            {
                Span <byte> magic = stackalloc byte[8];
                if (stream.Read(magic) != magic.Length || BitConverter.ToUInt64(magic) != SERIALIZATION_MAGIC_NUMBER)
                {
                    throw new PageLoadException();
                }
            } // magic goes out of scope

            using var gzip = new GZipStream(stream, CompressionMode.Decompress, true);

            int doIntRead()
            {
                Span <byte> intReadBuffer = stackalloc byte[4];

                if (gzip?.Read(intReadBuffer) != intReadBuffer.Length)
                {
                    throw new PageLoadException();
                }
                return(BitConverter.ToInt32(intReadBuffer));
            }

            int width  = doIntRead();
            int height = doIntRead();

            // read reserved values
            {
                Span <byte> reserved = stackalloc byte[16];
                if (gzip.Read(reserved) != reserved.Length)
                {
                    throw new PageLoadException();
                }
            } // reserved goes out of scope

            var page = new Page(width, height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    page._Cells[x, y] = doIntRead();
                }
            }
            return(page);
        }
Ejemplo n.º 13
0
    public static byte[] Decompress(byte[] b)
    {
        MemoryStream ms = new MemoryStream(b.Length);
        ms.Write(b, 0, b.Length);

        // last 4 bytes of GZipStream = length of decompressed data
        ms.Seek(-4, SeekOrigin.Current);
        byte[] lb = new byte[4];
        ms.Read(lb, 0, 4);
        int len = BitConverter.ToInt32(lb, 0);
        ms.Seek(0, SeekOrigin.Begin);

        byte[] ob = new byte[len];
        GZipStream zs = new GZipStream(ms, CompressionMode.Decompress);
        zs.Read(ob, 0, len);

        return ob;
    }
Ejemplo n.º 14
0
 static void Assemble(List<string> files, string destinationDirectory)
 {
     byte[] buffer = new byte[4096];
     int readBytes;
     string fileExtension = Source.Substring(Source.Length - 4);
     using (FileStream writingStream = new FileStream(destinationDirectory + "\\assembled" + fileExtension, FileMode.Create))
     {
         foreach (var file in files)
         {
             using (FileStream readingStream = new FileStream(file, FileMode.Open))
             {
                 using (GZipStream decompressionStream = new GZipStream(readingStream, CompressionMode.Decompress, false))
                 {
                     while ((readBytes = decompressionStream.Read(buffer, 0, buffer.Length)) != 0)
                     {
                         writingStream.Write(buffer, 0, readBytes);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 15
0
 private static void AssembleFiles(int i)
 {
     using (var source = new FileStream(string.Format("../../Part-{0}.txt", i), FileMode.Open))
     {
         using (var zip = new GZipStream(source, CompressionMode.Decompress))
         {
             using (var destination = new FileStream(assemblePath, i == 0 ? FileMode.Create : FileMode.Append))
             {
                 byte[] buffer = new byte[4096];
                 while (true)
                 {
                     int readBytes = zip.Read(buffer, 0, buffer.Length);
                     if (readBytes == 0)
                     {
                         break;
                     }
                     destination.Write(buffer, 0, readBytes);
                 }
             }
         }
     }
 }
Ejemplo n.º 16
0
 private static void AssembleFiles(int i)
 {
     using (var source = new FileStream(string.Format("../../Part-{0}.txt", i), FileMode.Open))
     {
         using (var zip = new GZipStream(source, CompressionMode.Decompress))//DEKOMPRESIRA
         {
             using (var destination = new FileStream(assemblePath, i == 0 ? FileMode.Create : FileMode.Append))
             {
                 byte[] buffer = new byte[4096];
                 while (true)
                 {
                     int readBytes = zip.Read(buffer, 0, buffer.Length);//chetem ot kompresirania fail
                     if (readBytes == 0)
                     {
                         break;
                     }
                     destination.Write(buffer, 0, readBytes);//zapisva dekompresiranite baitove v destination
                 }
             }
         }
     }
 }
 /// <summary>
 ///     A byte[] extension method that decompress the byte array gzip to string.
 /// </summary>
 /// <param name="this">The @this to act on.</param>
 /// <param name="encoding">The encoding.</param>
 /// <returns>The byte array gzip to string.</returns>
 public static string DecompressGZip(this byte[] @this, Encoding encoding)
 {
     const int bufferSize = 1024;
     using (var memoryStream = new MemoryStream(@this))
     {
         using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
         {
             // Memory stream for storing the decompressed bytes
             using (var outStream = new MemoryStream())
             {
                 var buffer = new byte[bufferSize];
                 int totalBytes = 0;
                 int readBytes;
                 while ((readBytes = zipStream.Read(buffer, 0, bufferSize)) > 0)
                 {
                     outStream.Write(buffer, 0, readBytes);
                     totalBytes += readBytes;
                 }
                 return encoding.GetString(outStream.GetBuffer(), 0, totalBytes);
             }
         }
     }
 }
Ejemplo n.º 18
0
    static void Decompress(string inputFile, string outputFile)
    {
        using (var inputStream = new FileStream(inputFile, FileMode.Open))
        {
            using (var compressionStream = new GZipStream(inputStream, CompressionMode.Decompress, false))
            {
                using (var outputStream = new FileStream(outputFile, FileMode.Create))
                {
                    byte[] buffer = new byte[4096];
                    while (true)
                    {
                        int readBytes = compressionStream.Read(buffer, 0, buffer.Length);
                        if (readBytes == 0)
                        {
                            break;
                        }

                        outputStream.Write(buffer, 0, readBytes);
                    }
                }
            }
        }
    }
Ejemplo n.º 19
0
    public static void DecompressFile(string toDecompressFileName, string targetFileName, bool IsDeleteSourceFile)
    {
        //文件流
        FileStream reader;
        reader = File.Open(toDecompressFileName, FileMode.Open);
        FileStream writer;
        writer = File.Create(targetFileName);

        //解压相关的流,同时向内存流中写数据
        byte[] sourceBuffer = new byte[reader.Length];
        reader.Read(sourceBuffer, 0, sourceBuffer.Length);
        MemoryStream ms = new MemoryStream(sourceBuffer);
        GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress, true);

        byte[] destBuffer = new byte[1];
        while (zipStream.Read(destBuffer, 0, destBuffer.Length) > 0)
        {
            writer.Write(destBuffer, 0, destBuffer.Length);
        }

        //释放并关闭解压流和内存流
        zipStream.Close();
        zipStream.Dispose();
        ms.Close();
        ms.Dispose();

        //关闭并释放文件流
        writer.Close();
        writer.Dispose();
        reader.Close();
        reader.Dispose();
        if (IsDeleteSourceFile)
        {
            File.Delete(toDecompressFileName);
        }
    }
    void DownloadAutoUpdater()
    {
        using (WebClient client = new WebClient())
        try
        {

            UnityEngine.Debug.Log ("Starting AutoUpdater Download");
            client.DownloadFile (url, path + Path.DirectorySeparatorChar + "AutomaticUpdater.zip");
            UnityEngine.Debug.Log ("Download Finished... Unzipping");

            using (Stream createFile = File.Create(path + Path.DirectorySeparatorChar + "AutomaticUpdater.app"))
            using (Stream openRead = File.OpenRead(path + Path.DirectorySeparatorChar + "AutomaticUpdater.zip"))
            using (Stream csStream = new GZipStream(openRead, CompressionMode.Decompress))
            {

                byte[] buffer = new byte[1024];
                int nRead;
                while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0)
                {

                    createFile.Write(buffer, 0, nRead);
                }
            }

        } catch (Exception error)
        {

            UnityEngine.Debug.Log (error);
            downloadErrorInfo = error;
            downloadError = true;
        }
    }
Ejemplo n.º 21
0
        public Map Load(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            using (FileStream mapStream = File.OpenRead(fileName)) {
                byte[] temp = new byte[8];
                Map    map  = null;

                mapStream.Seek(-4, SeekOrigin.End);
                mapStream.Read(temp, 0, 4);
                mapStream.Seek(0, SeekOrigin.Begin);
                int    uncompressedLength = BitConverter.ToInt32(temp, 0);
                byte[] data = new byte[uncompressedLength];
                using (GZipStream reader = new GZipStream(mapStream, CompressionMode.Decompress, true)) {
                    reader.Read(data, 0, uncompressedLength);
                }

                for (int i = 0; i < uncompressedLength - 1; i++)
                {
                    if (data[i] != 0xAC || data[i + 1] != 0xED)
                    {
                        continue;
                    }

                    // bypassing the header crap
                    int pointer = i + 6;
                    Array.Copy(data, pointer, temp, 0, 2);
                    pointer += IPAddress.HostToNetworkOrder(BitConverter.ToInt16(temp, 0));
                    pointer += 13;

                    int headerEnd;
                    // find the end of serialization listing
                    for (headerEnd = pointer; headerEnd < data.Length - 1; headerEnd++)
                    {
                        if (data[headerEnd] == 0x78 && data[headerEnd + 1] == 0x70)
                        {
                            headerEnd += 2;
                            break;
                        }
                    }

                    // start parsing serialization listing
                    int   offset = 0;
                    int   width = 0, length = 0, height = 0;
                    short x = 0,
                          y = 0,
                          z = 0;
                    while (pointer < headerEnd)
                    {
                        switch ((char)data[pointer])
                        {
                        case 'Z':
                            offset++;
                            break;

                        case 'F':
                        case 'I':
                            offset += 4;
                            break;

                        case 'J':
                            offset += 8;
                            break;
                        }

                        pointer += 1;
                        Array.Copy(data, pointer, temp, 0, 2);
                        short skip = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(temp, 0));
                        pointer += 2;

                        // look for relevant variables
                        Array.Copy(data, headerEnd + offset - 4, temp, 0, 4);
                        if (BufferUtil.MemCmp(data, pointer, "width"))
                        {
                            width = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                        }
                        else if (BufferUtil.MemCmp(data, pointer, "depth"))
                        {
                            height = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                        }
                        else if (BufferUtil.MemCmp(data, pointer, "height"))
                        {
                            length = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                        }
                        else if (BufferUtil.MemCmp(data, pointer, "xSpawn"))
                        {
                            x = (short)(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                        }
                        else if (BufferUtil.MemCmp(data, pointer, "ySpawn"))
                        {
                            y = (short)(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                        }
                        else if (BufferUtil.MemCmp(data, pointer, "zSpawn"))
                        {
                            z = (short)(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                        }

                        pointer += skip;
                    }

                    map = new Map(null, width, length, height, false)
                    {
                        Spawn = new Position(x, y, z)
                    };

                    // find the start of the block array
                    bool foundBlockArray = false;
                    offset = Array.IndexOf <byte>(data, 0x00, headerEnd);
                    while (offset != -1 && offset < data.Length - 2)
                    {
                        if (data[offset] == 0x00 && data[offset + 1] == 0x78 && data[offset + 2] == 0x70)
                        {
                            foundBlockArray = true;
                            pointer         = offset + 7;
                        }
                        offset = Array.IndexOf <byte>(data, 0x00, offset + 1);
                    }

                    // copy the block array... or fail
                    if (foundBlockArray)
                    {
                        map.Blocks = new byte[map.Volume];
                        Array.Copy(data, pointer, map.Blocks, 0, map.Blocks.Length);
                        map.ConvertBlockTypes(Mapping);
                    }
                    else
                    {
                        throw new MapFormatException("MapDAT: Could not locate block array.");
                    }
                    break;
                }

                if (map == null)
                {
                    throw new MapFormatException("MapDAT: No data.");
                }
                return(map);
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 读取请求返回的数据
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns></returns>
        private byte[] GetData(HttpWebRequest request)
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream          stream   = response.GetResponseStream();

            responseHeaders = response.Headers;
            //SaveCookiesToDisk();

            DownloadEventArgs args = new DownloadEventArgs();

            if (responseHeaders[HttpResponseHeader.ContentLength] != null)
            {
                args.TotalBytes = Convert.ToInt32(responseHeaders[HttpResponseHeader.ContentLength]);
            }

            MemoryStream ms    = new MemoryStream();
            int          count = 0;

            byte[] buf = new byte[bufferSize];
            while ((count = stream.Read(buf, 0, buf.Length)) > 0)
            {
                ms.Write(buf, 0, count);
                if (this.DownloadProgressChanged != null)
                {
                    args.BytesReceived += count;
                    args.ReceivedData   = new byte[count];
                    Array.Copy(buf, args.ReceivedData, count);
                    this.DownloadProgressChanged(this, args);
                }
            }
            stream.Close();
            //解压
            if (ResponseHeaders[HttpResponseHeader.ContentEncoding] != null)
            {
                MemoryStream msTemp = new MemoryStream();
                count = 0;
                buf   = new byte[100];
                switch (ResponseHeaders[HttpResponseHeader.ContentEncoding].ToLower())
                {
                case "gzip":
                    GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
                    while ((count = gzip.Read(buf, 0, buf.Length)) > 0)
                    {
                        msTemp.Write(buf, 0, count);
                    }
                    return(msTemp.ToArray());

                case "deflate":
                    DeflateStream deflate = new DeflateStream(ms, CompressionMode.Decompress);
                    while ((count = deflate.Read(buf, 0, buf.Length)) > 0)
                    {
                        msTemp.Write(buf, 0, count);
                    }
                    return(msTemp.ToArray());

                default:
                    break;
                }
            }
            return(ms.ToArray());
        }
Ejemplo n.º 23
0
        private static FileStream UnzipToTempFile(string lpZipFile, string lpTempFile, GZipResult result)
        {
            FileStream fsIn   = null;
            GZipStream gzip   = null;
            FileStream fsOut  = null;
            FileStream fsTemp = null;

            const int bufferSize = 4096;

            byte[] buffer = new byte[bufferSize];
            int    count  = 0;

            try
            {
                fsIn = new FileStream(lpZipFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                result.ZipFileSize = fsIn.Length;

                fsOut = new FileStream(lpTempFile, FileMode.Create, FileAccess.Write, FileShare.None);
                gzip  = new GZipStream(fsIn, CompressionMode.Decompress, true);
                while (true)
                {
                    count = gzip.Read(buffer, 0, bufferSize);
                    if (count != 0)
                    {
                        fsOut.Write(buffer, 0, count);
                    }
                    if (count != bufferSize)
                    {
                        break;
                    }
                }
            }
            catch //(Exception ex1)
            {
                result.Errors = true;
            }
            finally
            {
                if (gzip != null)
                {
                    gzip.Close();
                    gzip = null;
                }
                if (fsOut != null)
                {
                    fsOut.Close();
                    fsOut = null;
                }
                if (fsIn != null)
                {
                    fsIn.Close();
                    fsIn = null;
                }
            }

            fsTemp = new FileStream(lpTempFile, FileMode.Open, FileAccess.Read, FileShare.None);
            if (fsTemp != null)
            {
                result.TempFileSize = fsTemp.Length;
            }
            return(fsTemp);
        }
Ejemplo n.º 24
0
        public byte[] GetFsData(string hash)
        {
            string spoolFile = Path.Combine(m_SpoolDirectory, hash + ".asset");

            if (File.Exists(spoolFile))
            {
                try
                {
                    byte[] content = File.ReadAllBytes(spoolFile);

                    return(content);
                }
                catch
                {
                }
            }

            string file     = HashToFile(hash);
            string diskFile = Path.Combine(m_FSBase, file);

            if (File.Exists(diskFile + ".gz"))
            {
                try
                {
                    using (GZipStream gz = new GZipStream(new FileStream(diskFile + ".gz", FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            byte[] data = new byte[32768];
                            int    bytesRead;

                            do
                            {
                                bytesRead = gz.Read(data, 0, 32768);
                                if (bytesRead > 0)
                                {
                                    ms.Write(data, 0, bytesRead);
                                }
                            } while (bytesRead > 0);

                            return(ms.ToArray());
                        }
                    }
                }
                catch (Exception)
                {
                    return(new Byte[0]);
                }
            }
            else if (File.Exists(diskFile))
            {
                try
                {
                    byte[] content = File.ReadAllBytes(diskFile);

                    return(content);
                }
                catch
                {
                }
            }
            return(new Byte[0]);
        }
Ejemplo n.º 25
0
    private static byte[] UnGzip(byte[] data, int start)
    {
        int size = BitConverter.ToInt32(data, data.Length - 4);
        byte[] uncompressedData = new byte[size];
        MemoryStream memStream = new MemoryStream(data, start, (data.Length - start));
        memStream.Position = 0;
        GZipStream gzStream = new GZipStream(memStream, CompressionMode.Decompress);

        try
        {
            gzStream.Read(uncompressedData, 0, size);
        }
        catch (Exception)
        {
            throw;
        }

        gzStream.Close();
        return uncompressedData;
    }
Ejemplo n.º 26
0
    public static byte[] decompress_zip(byte[] data)
    {
        MemoryStream ms = new MemoryStream(data);

        GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);

        byte[] data_total = new byte[40 * 1024];
        long total = 0;

        byte[] buffer = new byte[8];
        int count = 0;
        do
        {
            count = stream.Read(buffer, 0, 8);
            if (data_total.Length <= total + count) //放大数组
            {
                byte[] temp = new byte[data_total.Length * 10];
                data_total.CopyTo(temp, 0);
                data_total = temp;
            }
            buffer.CopyTo(data_total, total);
            total += count;
        } while (count != 0);
        byte[] data_desc = new byte[total];
        Array.Copy(data_total, 0, data_desc, 0, total);
        return data_desc;
    }
Ejemplo n.º 27
0
        public void _request(string endPoint, RequestData requestData)
        {
            nonce    += 1;
            isSending = true;

            if (LastRequest == null || endPoint != LastRequest.EndPoint || !requestData.Equals(LastRequest.RequestData))
            {
                requestData.cacheKey = UnityEngine.Random.Range(1000, 9999).ToString();
            }

            LastRequest             = new LastRequest();
            LastRequest.EndPoint    = endPoint;
            LastRequest.RequestData = requestData;

            requestData.version    = Version;
            requestData.versionKey = VersionKey;
            requestData.timestamp  = PredictedServerTimestamp;
            requestData.session    = user.session;
            //requestData.cacheKey

            // post
            Byte[] payload   = Encoding.UTF8.GetBytes(JsonUtility.ToJson(requestData));   // Json(requestData).toBytes(); new Byte[ 100 ]
            Byte[] encrypted = _encryptionHelper.Encrypt(payload, nonce);
            if (localSendInvalidRequestData)
            {
                encrypted = Encoding.UTF8.GetBytes("1234");
            }
            string payloadBase64 = Convert.ToBase64String(encrypted);   // "encryptWithChaCha(payload).toBase64()";
            var    postForm      = new RawJsonForm();

            postForm.AddField("payloadBase64", payloadBase64);

            // signature
            Byte[] digest      = md5.ComputeHash(encrypted);                 // md5(payload);new Byte[ 100 ]
            Byte[] nonceByte   = BitConverter.GetBytes(nonce);               // nonce to byte new Byte[ 8 ]
            Byte[] signMessage = new Byte[nonceByte.Length + digest.Length]; // nonceByte + digest
            Buffer.BlockCopy(nonceByte, 0, signMessage, 0, nonceByte.Length);
            Buffer.BlockCopy(digest, 0, signMessage, nonceByte.Length, digest.Length);
            if (localSendInvalidSignBase64)
            {
                signMessage = Encoding.UTF8.GetBytes("1234");
            }
            string signedBase64 = Convert.ToBase64String(_encryptionHelper.SignMessage(signMessage));     //"signWithChaCha(signMessage).toBase64()";

            // query
            Dictionary <string, string> queryDict = new Dictionary <string, string>();

            queryDict.Add("signedBase64", signedBase64);
            queryDict.Add("token", token);
            queryDict.Add("remoteTimeout", remoteTimeout.ToString());
            queryDict.Add("remoteSendInvalidPayload", remoteSendInvalidPayload.ToString());
            var query = getQuery(queryDict);

            // send request
            var         url         = $"{Host}{endPoint}?{query}";
            RespondData respondData = new RespondData();
            HTTPRequest request     = new HTTPRequest(new Uri(url), HTTPMethods.Post, (originalRequest, response) =>
            {
                isSending         = false;
                receivedTimestamp = CurrentTimestamp;

                if (response == null)
                {
                    this.RespondData      = new RespondData();
                    RespondData.errorCode = ErrorCode.Timeout;
                    return;
                }
                if (response.Data.Length <= 10)
                {
                    this.RespondData      = new RespondData();
                    RespondData.errorCode = ErrorCode.FailedToDecryptServerPayload;
                    return;
                }
                //Debug.Log( response.HasHeaderWithValue( "Content-Encoding", "encrypted" ) );
                const int size = 4096;
                byte[] buffer  = new byte[size];
                if (response.HasHeaderWithValue("Content-Encoding", "encrypted"))
                {
                    byte[] data;

                    using (GZipStream stream = new GZipStream(new MemoryStream(_encryptionHelper.Decrypt(response.Data, nonce)), CompressionMode.Decompress))
                    {
                        using (MemoryStream memory = new MemoryStream())
                        {
                            int count = 0;
                            do
                            {
                                count = stream.Read(buffer, 0, size);
                                if (count > 0)
                                {
                                    memory.Write(buffer, 0, count);
                                }
                            }while(count > 0);
                            data = memory.ToArray();
                        }
                    }
                    respondData = JsonUtility.FromJson <RespondData>(Encoding.UTF8.GetString(buffer));
                }
                else
                {
                    respondData = JsonUtility.FromJson <RespondData>(response.DataAsText);
                }
                this.RespondData = respondData;
                if (respondData.errorCode == 0)
                {
                    if (!String.IsNullOrEmpty(respondData.token))
                    {
                        this.token = respondData.token;
                    }
                    this.user            = respondData.user;
                    this.card            = respondData.body.card;
                    this.cards           = respondData.body.cards;
                    this.serverTimestamp = respondData.timestamp;
                }
            });

            request.SetForm(postForm);
            request.Timeout = TimeSpan.FromSeconds(Timeout);
            request.Send();
        }
Ejemplo n.º 28
0
        public static Level Load(Stream lvlStream, string fileName)
        {
            byte[] temp = new byte[8];
            Level  lvl  = new Level(fileName, 0, 0, 0, "empty");

            lvl.world = "main";
            byte[] data;
            int    length;

            try {
                lvlStream.Seek(-4, SeekOrigin.End);
                lvlStream.Read(temp, 0, sizeof(int));
                lvlStream.Seek(0, SeekOrigin.Begin);
                length = BitConverter.ToInt32(temp, 0);
                data   = new byte[length];
                using (GZipStream reader = new GZipStream(lvlStream, CompressionMode.Decompress, true)) {
                    reader.Read(data, 0, length);
                }

                for (int i = 0; i < length - 1; i++)
                {
                    if (data[i] == 0xAC && data[i + 1] == 0xED)
                    {
                        // bypassing the header crap
                        int pointer = i + 6;
                        Array.Copy(data, pointer, temp, 0, sizeof(short));
                        pointer += IPAddress.HostToNetworkOrder(BitConverter.ToInt16(temp, 0));
                        pointer += 13;

                        int headerEnd = 0;
                        // find the end of serialization listing
                        for (headerEnd = pointer; headerEnd < data.Length - 1; headerEnd++)
                        {
                            if (data[headerEnd] == 0x78 && data[headerEnd + 1] == 0x70)
                            {
                                headerEnd += 2;
                                break;
                            }
                        }

                        // start parsing serialization listing
                        int offset = 0;
                        while (pointer < headerEnd)
                        {
                            if (data[pointer] == 'Z')
                            {
                                offset++;
                            }
                            else if (data[pointer] == 'I' || data[pointer] == 'F')
                            {
                                offset += 4;
                            }
                            else if (data[pointer] == 'J')
                            {
                                offset += 8;
                            }

                            pointer += 1;
                            Array.Copy(data, pointer, temp, 0, sizeof(short));
                            short skip = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(temp, 0));
                            pointer += 2;

                            // look for relevant variables
                            Array.Copy(data, headerEnd + offset - 4, temp, 0, sizeof(int));
                            if (MemCmp(data, pointer, "width"))
                            {
                                lvl.width = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "depth"))
                            {
                                lvl.depth = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "height"))
                            {
                                lvl.height = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                            }

                            pointer += skip;
                        }

                        lvl.spawnx = (ushort)(lvl.width / 1.3);
                        lvl.spawny = (ushort)(lvl.depth / 1.3);
                        lvl.spawnz = (ushort)(lvl.height / 1.3);

                        // find the start of the block array
                        bool foundBlockArray = false;
                        offset = Array.IndexOf <byte>(data, 0x00, headerEnd);
                        while (offset != -1 && offset < data.Length - 2)
                        {
                            if (data[offset] == 0x00 && data[offset + 1] == 0x78 && data[offset + 2] == 0x70)
                            {
                                foundBlockArray = true;
                                pointer         = offset + 7;
                            }
                            offset = Array.IndexOf <byte>(data, 0x00, offset + 1);
                        }

                        // copy the block array... or fail
                        if (foundBlockArray)
                        {
                            lvl.CopyBlocks(data, pointer);
                            lvl.Save(true);
                        }
                        else
                        {
                            throw new Exception("Could not locate block array.");
                        }
                        break;
                    }
                }
            } catch (Exception ex) {
                Server.s.Log("Conversion failed");
                Server.ErrorLog(ex);
                return(null);
            }

            return(lvl);
        }
Ejemplo n.º 29
0
        public bool LoadDocument(string documentFilePath)
        {
            this.UnloadDocument();

            if (textEditor == null || string.IsNullOrWhiteSpace(documentFilePath))
            {
                return(false);
            }

            string fileExt = Path.GetExtension(documentFilePath);

            if (string.IsNullOrWhiteSpace(fileExt))
            {
                return(false);
            }
            if (!string.Equals(fileExt, ".xaml", StringComparison.OrdinalIgnoreCase) &&
                !string.Equals(fileExt, ".zaml", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (string.Equals(fileExt, ".zaml", StringComparison.OrdinalIgnoreCase))
            {
                using (FileStream fileStream = File.OpenRead(documentFilePath))
                {
                    using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Decompress))
                    {
                        // Text Editor does not work with this stream, so we read the data to memory stream...
                        MemoryStream memoryStream = new MemoryStream();
                        // Use this method is used to read all bytes from a stream.
                        int    totalCount = 0;
                        int    bufferSize = 512;
                        byte[] buffer     = new byte[bufferSize];
                        while (true)
                        {
                            int bytesRead = zipStream.Read(buffer, 0, bufferSize);
                            if (bytesRead == 0)
                            {
                                break;
                            }
                            memoryStream.Write(buffer, 0, bytesRead);
                            totalCount += bytesRead;
                        }

                        if (totalCount > 0)
                        {
                            memoryStream.Position = 0;
                        }

                        textEditor.Load(memoryStream);

                        memoryStream.Close();
                    }
                }
            }
            else
            {
                textEditor.Load(documentFilePath);
            }

            if (_foldingManager == null || _foldingStrategy == null)
            {
                _foldingManager  = FoldingManager.Install(textEditor.TextArea);
                _foldingStrategy = new XmlFoldingStrategy();
            }

            _foldingStrategy.UpdateFoldings(_foldingManager, textEditor.Document);

            return(true);
        }
Ejemplo n.º 30
0
        public bool LoadDocument(string documentFilePath, SvgTestInfo testInfo, object extraInfo = null)
        {
            this.UnloadDocument();

            if (string.IsNullOrWhiteSpace(documentFilePath) || testInfo == null)
            {
                return(false);
            }

            if (extraInfo != null)
            {
                _optionSettings = extraInfo as OptionSettings;
            }
            _svgFilePath = documentFilePath;

            bool isLoaded = false;

            string fileExt = Path.GetExtension(documentFilePath);

            if (string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
            {
                using (FileStream fileStream = File.OpenRead(documentFilePath))
                {
                    using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Decompress))
                    {
                        // Text Editor does not work with this stream, so we read the data to memory stream...
                        MemoryStream memoryStream = new MemoryStream();
                        // Use this method is used to read all bytes from a stream.
                        int    totalCount = 0;
                        int    bufferSize = 512;
                        byte[] buffer     = new byte[bufferSize];
                        while (true)
                        {
                            int bytesRead = zipStream.Read(buffer, 0, bufferSize);
                            if (bytesRead == 0)
                            {
                                break;
                            }

                            memoryStream.Write(buffer, 0, bytesRead);
                            totalCount += bytesRead;
                        }

                        if (totalCount > 0)
                        {
                            memoryStream.Position = 0;
                        }

                        isLoaded = this.LoadFile(memoryStream, testInfo);

                        memoryStream.Close();
                    }
                }
            }
            else
            {
                using (FileStream stream = File.OpenRead(documentFilePath))
                {
                    isLoaded = this.LoadFile(stream, testInfo);
                }
            }

            btnFilePath.IsEnabled = isLoaded;

            return(isLoaded);
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Returns a new Board object
        /// </summary>
        /// <param name="board">A previously serialized and compressed board object</param>
        /// <returns>BOARD: A board from the parsed byte array</returns>
        public static Board UnSerialize(IEnumerable <byte> board)
        {
            var bytes = new Queue <byte>(board);

            // Remove the first 4 bytes for Black colour
            var intBytes = new byte[4];

            for (var i = 0; i < 4; i++)
            {
                intBytes[i] = bytes.Dequeue();
            }
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(intBytes);
            }
            var bCol = Color.FromArgb(BitConverter.ToInt32(intBytes.ToArray(), 0));

            // Remove the next 4 bytes for White colour
            intBytes = new byte[4];
            for (var i = 0; i < 4; i++)
            {
                intBytes[i] = bytes.Dequeue();
            }
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(intBytes);
            }
            var wCol = Color.FromArgb(BitConverter.ToInt32(intBytes.ToArray(), 0));

            // Remove next byte for if it is player one's turn
            var playerOne = Convert.ToBoolean(bytes.Dequeue());
            // Remove next byte for game mode
            var mode = (Gametype)bytes.Dequeue();

            // Remove 8 byte for the Last Move
            var lastMove = new byte[4];

            for (var i = 0; i < 4; i++)
            {
                lastMove[i] = bytes.Dequeue();
            }
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(lastMove);
            }
            var lastMoveStart = BitConverter.ToInt32(lastMove.ToArray(), 0);

            lastMove = new byte[4];
            for (var i = 0; i < 4; i++)
            {
                lastMove[i] = bytes.Dequeue();
            }
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(lastMove);
            }
            var lastMoveEnd = BitConverter.ToInt32(lastMove.ToArray(), 0);

            intBytes = new byte[4];
            for (var i = 0; i < 4; i++)
            {
                intBytes[i] = bytes.Dequeue();
            }
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(intBytes);
            }
            var fifty = BitConverter.ToInt32(intBytes.ToArray(), 0);

            var takenCount = bytes.Dequeue();

            // Uncompress the byte array
            List <Piece> pieces;

            byte[] deserialize;
            using (var stream = new GZipStream(new MemoryStream(bytes.ToArray()), CompressionMode.Decompress)) {
                const int size   = 4096;
                var       buffer = new byte[size];
                using (var memory = new MemoryStream()) {
                    int count;
                    do
                    {
                        count = stream.Read(buffer, 0, size);
                        if (count > 0)
                        {
                            memory.Write(buffer, 0, count);
                        }
                    }while (count > 0);
                    deserialize = memory.ToArray();
                }
            }

            // Deserialize uncompressed byte array to a list of Pieces
            using (var stream = new MemoryStream(deserialize))
                pieces = (List <Piece>) new BinaryFormatter().Deserialize(stream);

            var takenpieces = pieces.Skip(pieces.Count - takenCount).ToList();

            pieces.RemoveRange(pieces.Count - takenCount, takenCount);

            // Create a new board from all the information
            var b = new Board(wCol, bCol, mode, pieces)
            {
                PlayerOneTurn = playerOne,
                LastMove      = Tuple.Create(lastMoveStart, lastMoveEnd),
                LastPiece     = pieces[pieces.Count - 1],
                TakenPieces   = takenpieces,
                FiftyMoveRule = fifty
            };

            // Remove LastPiece from pieces
            b.Pieces.Remove(b.LastPiece);

            // Return new board
            return(b);
        }
Ejemplo n.º 32
0
        public void LoadDataFromServer(string url, string filename, Action <string> callback, bool compressData = true)
        {
            var root = Application.persistentDataPath.TrimEnd(Path.DirectorySeparatorChar);

            var fullpath = root + Path.DirectorySeparatorChar + filename + (compressData ? ".zipc4a" : ".c4a");

            if (!File.Exists(fullpath))
            {
                var baseUrl = HostUrl;
                if (!Application.isMobilePlatform)
                {
                    baseUrl = "http://localhost:1234";
                }

                HttpWebRequest wr = new HttpWebRequest(new Uri(baseUrl + "/" + url + (compressData ? "?compress=true" : "")));
                wr.Method        = "GET";
                wr.ContentLength = 0;
                wr.ContentType   = "text/html";
                using (var stream = wr.GetResponse().GetResponseStream()) {
                    using (var save = new FileStream(fullpath, FileMode.Create)) {
                        byte[] buffer = new byte[1024];
                        while (true)
                        {
                            int n = stream.Read(buffer, 0, buffer.Length);
                            if (n == 0)
                            {
                                break;
                            }
                            save.Write(buffer, 0, n);
                        }
                        save.Flush();
                    }
                }
            }

            var path = root + Path.DirectorySeparatorChar + filename + ".c4a";

            //decompress data
            if (File.Exists(fullpath) && compressData && !File.Exists(path))
            {
                using (var save = new FileStream(path, FileMode.Create)) {
                    using (var stream = new GZipStream(new FileStream(fullpath, FileMode.Open), CompressionMode.Decompress)) {
                        byte[] buffer = new byte[1024];
                        while (true)
                        {
                            int n = stream.Read(buffer, 0, buffer.Length);
                            if (n == 0)
                            {
                                break;
                            }
                            save.Write(buffer, 0, n);
                        }
                        save.Flush();
                    }
                }
            }

            if (File.Exists(path) && callback != null)
            {
                callback(path);
            }
        }
Ejemplo n.º 33
0
        public void Load(string fileName, IFMap map)
        {
            log.Log("Converting {0}...", FLogType.SystemActivity, fileName);
            byte[] temp = new byte[8];
            //Map map = new Map(world);
            byte[] data;
            int length;
            try
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    stream.Seek(-4, SeekOrigin.End);
                    stream.Read(temp, 0, sizeof(int));
                    stream.Seek(0, SeekOrigin.Begin);
                    length = BitConverter.ToInt32(temp, 0);
                    data = new byte[length];
                    using (GZipStream reader = new GZipStream(stream, CompressionMode.Decompress))
                    {
                        reader.Read(data, 0, length);
                    }
                }

                //if( data[0] == 0xBE && data[1] == 0xEE && data[2] == 0xEF ) {
                for (int i = 0; i < length - 1; i++)
                {
                    if (data[i] == 0xAC && data[i + 1] == 0xED)
                    {

                        // bypassing the header crap
                        int pointer = i + 6;
                        Array.Copy(data, pointer, temp, 0, sizeof(short));
                        pointer += IPAddress.HostToNetworkOrder(BitConverter.ToInt16(temp, 0));
                        pointer += 13;

                        int headerEnd = 0;
                        // find the end of serialization listing
                        for (headerEnd = pointer; headerEnd < data.Length - 1; headerEnd++)
                        {
                            if (data[headerEnd] == 0x78 && data[headerEnd + 1] == 0x70)
                            {
                                headerEnd += 2;
                                break;
                            }
                        }

                        // start parsing serialization listing
                        int offset = 0;
                        while (pointer < headerEnd)
                        {
                            if (data[pointer] == 'Z') offset++;
                            else if (data[pointer] == 'I' || data[pointer] == 'F') offset += 4;
                            else if (data[pointer] == 'J') offset += 8;

                            pointer += 1;
                            Array.Copy(data, pointer, temp, 0, sizeof(short));
                            short skip = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(temp, 0));
                            pointer += 2;

                            // look for relevant variables
                            Array.Copy(data, headerEnd + offset - 4, temp, 0, sizeof(int));
                            if (MemCmp(data, pointer, "width"))
                            {
                                map.MapSizeX = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "depth"))
                            {
                                map.MapSizeZ = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "height"))
                            {
                                map.MapSizeY = (ushort)IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0));
                            }
                            else if (MemCmp(data, pointer, "xSpawn"))
                            {
                                map.SpawnX = (short)(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                            }
                            else if (MemCmp(data, pointer, "ySpawn"))
                            {
                                map.SpawnZ = (short)(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                            }
                            else if (MemCmp(data, pointer, "zSpawn"))
                            {
                                map.SpawnY = (short)(IPAddress.HostToNetworkOrder(BitConverter.ToInt32(temp, 0)) * 32 + 16);
                            }

                            pointer += skip;
                        }

                        // find the start of the block array
                        bool foundBlockArray = false;
                        offset = Array.IndexOf<byte>(data, 0x00, headerEnd);
                        while (offset != -1 && offset < data.Length - 2)
                        {
                            if (data[offset] == 0x00 && data[offset + 1] == 0x78 && data[offset + 2] == 0x70)
                            {
                                foundBlockArray = true;
                                pointer = offset + 7;
                            }
                            offset = Array.IndexOf<byte>(data, 0x00, offset + 1);
                        }

                        // copy the block array... or fail
                        if (foundBlockArray)
                        {
                            map.LoadMapArray(data, pointer);
                            if (!map.ValidateBlockTypes())
                            {
                                throw new Exception("Map validation failed: unknown block types found. Either parsing has done wrong, or this is an incompatible format.");
                            }
                        }
                        else
                        {
                            throw new Exception("Could not locate block array.");
                        }
                        break;
                    }
                    //}
                }
            }
            catch (Exception ex)
            {
                log.Log("Conversion failed: {0}", FLogType.Error, ex.Message);
                log.Log(ex.StackTrace, FLogType.Debug);
            }
            log.Log("Conversion completed succesfully succesful.", FLogType.SystemActivity, fileName);
        }
Ejemplo n.º 34
0
            public static Byte[] DecompressArray(Byte[] inBuf)
            {
                Logger.Enter();
                MemoryStream memory = new MemoryStream();

                try
                {
                    using (GZipStream stream = new GZipStream(new MemoryStream(inBuf), CompressionMode.Decompress))
                    {
                        const int size = 4096;
                        byte[] buffer = new byte[size];

                        int count = 0;
                        do
                        {
                            count = stream.Read(buffer, 0, size);
                            if (count > 0)
                            {
                                memory.Write(buffer, 0, count);
                            }
                        }
                        while (count > 0);
                    }
                }
                catch(Exception ex)
                {
                    Logger.Error(String.Format("Decompress error.Internal msg: {0}", ex.Message));
                }

                Logger.Leave();
                return memory.ToArray();
            }
Ejemplo n.º 35
0
        /// <summary>
        /// Handle the decryption of the identified filepath to an output directory
        /// </summary>
        /// <returns>Returns true of the operation was successful</returns>
        public override bool StartOperation()
        {
            //Check that there is a single file to process
            if (identifiedFiles.Count != 1)
            {
                Console.WriteLine($"Can't start decryption operation, there is an invalid number ({identifiedFiles.Count}) of files set as the target. Expected only 1");
                return(false);
            }

            //Store the buffers that will be used for the operation
            FileStream fStream = null;
            GZipStream reader  = null;
            FileStream writer  = null;

            //Create the encryption key that will be used for this operation
            VigenèreCipherKey key = new VigenèreCipherKey(Key);

            //Flag if the operation was completed successfully
            bool successful = true;

            //Store a progress value that can be output
            float progress = 0f;

            //Store a collection of the files that are generated by this operation
            //<Temp Path, Final Path>
            List <Tuple <string, string> > generatedFiles = new List <Tuple <string, string> >();

            //Attempt to decrypt the entire file
            try {
                //Create the initial buffer objects
                fStream = new FileStream(TargetPath, FileMode.Open, FileAccess.Read, FileShare.Read, BUFFER_SIZE);
                reader  = new GZipStream(fStream, CompressionMode.Decompress);

                //Create the byte buffers that will be used for the operation
                byte[] dataBuffer = new byte[BUFFER_SIZE];
                byte[] intBuffer  = new byte[sizeof(int)];
                byte[] longBuffer = new byte[sizeof(long)];

                //Read the number of files that are included in this collection
                reader.Read(intBuffer, 0, sizeof(int));
                key.Decrypt(ref intBuffer, sizeof(int));

                //Decrypt the count
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(intBuffer, 0, sizeof(int));
                }
                int fileCount = BitConverter.ToInt32(intBuffer, 0);

                //Calculate the percentage to use for each file processed
                float percentageUsage = .75f / fileCount;

                //Loop through each file to be processed
                for (int i = 0; i < fileCount; i++)
                {
                    //Read the bytes for the number of characters in the filepath
                    reader.Read(intBuffer, 0, sizeof(int));
                    key.Decrypt(ref intBuffer, sizeof(int));

                    //Get the number of characters that are in the
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(intBuffer, 0, sizeof(int));
                    }
                    int characterCount = BitConverter.ToInt32(intBuffer, 0);

                    //Construct the relative filepath back from the data
                    StringBuilder relativePath = new StringBuilder(characterCount);

                    //Loop through and read the filepath
                    long processedCount = 0;
                    do
                    {
                        //Retrieve the next chunk of characters from datafile
                        int readCount = reader.Read(dataBuffer, 0,
                                                    Math.Min(
                                                        (BUFFER_SIZE / sizeof(char)) * sizeof(char),
                                                        (characterCount - (int)processedCount) * sizeof(char)
                                                        )
                                                    );

                        //Decrypt the character bytes
                        key.Decrypt(ref dataBuffer, readCount);

                        //Half the count for final processing
                        readCount /= sizeof(char);

                        //Extract the characters from the buffer
                        byte[] charBuffer = new byte[sizeof(char)];
                        for (int c = 0; c < readCount; c++)
                        {
                            //Get the character bytes from the array
                            Array.Copy(dataBuffer, c * sizeof(char), charBuffer, 0, sizeof(char));

                            //Convert the byte data back to a character
                            if (BitConverter.IsLittleEndian)
                            {
                                Array.Reverse(charBuffer, 0, sizeof(char));
                            }
                            relativePath.Append(BitConverter.ToChar(charBuffer, 0));
                        }

                        //Increase the counter
                        processedCount += readCount;
                    } while (processedCount < characterCount);

                    //Get the amount of data to evaluated by this process
                    reader.Read(longBuffer, 0, sizeof(long));
                    key.Decrypt(ref longBuffer, sizeof(long));

                    //Get the amount of data to be processed
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(longBuffer, 0, sizeof(long));
                    }
                    long dataCount = BitConverter.ToInt64(longBuffer, 0);

                    //Get a temp file to store the data at
                    string tempPath = Path.GetTempFileName();

                    //Add the entry to the monitor list
                    generatedFiles.Add(new Tuple <string, string>(
                                           tempPath,
                                           Path.Combine(DestinationPath, relativePath.ToString())
                                           ));

                    //Try to create process the contained file
                    try {
                        //Open the temporary file for writing
                        writer = new FileStream(tempPath, FileMode.Append, FileAccess.Write, FileShare.Read, BUFFER_SIZE);

                        //Process all of the data within the file
                        processedCount = 0;
                        do
                        {
                            //Retrieve the next chunk of data to process
                            int readCount = reader.Read(dataBuffer, 0,
                                                        (int)Math.Min(
                                                            BUFFER_SIZE,
                                                            dataCount - processedCount
                                                            )
                                                        );

                            //If there is data to be read but nothing was read from the file, then something has gone wrong
                            if (readCount == 0 && dataCount - processedCount > 0)
                            {
                                throw new OperationCanceledException($"OperationCanceledException: {dataCount - processedCount} bytes are left to be read but 0 bytes were read. Reached EOF");
                            }

                            //Increase the counters
                            processedCount += readCount;

                            //Decrypt the buffer and write it to the file
                            key.Decrypt(ref dataBuffer, readCount);
                            writer.Write(dataBuffer, 0, readCount);
                        } while (processedCount < dataCount);
                    }
#if DEBUG
                    //Log the exception thrown for debugging purposes
                    catch (Exception exec) {
                        Console.WriteLine($"Decryption failed to process internal file. Writing of included file '{relativePath.ToString()}' failed. ERROR: {exec.Message}");
                        successful = false;
                        break;
                    }
#else
                    //Log general failure on exception. Assume key is wrong
                    catch {
                        Console.WriteLine("Decryption failed to process an internal file. Is the Cipher Key correct?");
                        successful = false;
                        break;
                    }
#endif
                    //Cleanup the file writing
                    finally { if (writer != null)
                              {
                                  writer.Dispose(); writer = null;
                              }
                    }

                    //Increment the operation percentage
                    progress += percentageUsage;
                    Console.WriteLine($"\tProgress: {(progress * 100f).ToString("F2") + '%'}");
                }
            }

#if DEBUG
            //Log the exception thrown for debugging purposes
            catch (Exception exec) {
                Console.WriteLine($"Decryption failed to process internal file. Is the Cipher Key correct? ERROR: {exec.Message}");
                successful = false;
            }
#else
            //Log general failure on exception. Assume key is wrong
            catch {
                Console.WriteLine("Decryption failed to process an internal file. Is the Cipher Key correct?");
                successful = false;
            }
#endif

            finally {
                //Clear out the file streams
                if (writer != null)
                {
                    writer.Dispose(); writer = null;
                }
                if (reader != null)
                {
                    reader.Dispose(); reader = null;
                }
                if (fStream != null)
                {
                    fStream.Dispose(); fStream = null;
                }

                //Loop for the duration of the cleanup
                while (true)
                {
                    //If the operation failed, delete all of the temp files
                    if (!successful)
                    {
                        //Delete all of the (remaining) temp files
                        for (int i = 0; i < generatedFiles.Count; i++)
                        {
                            try { File.Delete(generatedFiles[i].Item1); } catch { }
                        }

                        //Break from the infinite loop
                        break;
                    }

                    //Otherwise, shift the files to the valid destination
                    else
                    {
                        //Calculate a percentage requirement for each file to shift
                        float percentageUsage = .25f / generatedFiles.Count;

                        //Process all of the files
                        string destinationPath = string.Empty;
                        for (int i = 0; i < generatedFiles.Count; i++)
                        {
                            //Get the next filepath that is to be processed by the data
                            destinationPath = generatedFiles[i].Item2;

                            //Attempt to ensure the directory exists, catching filepaths that are to long
                            bool attempted = false, failed = false;
                            while (true)
                            {
                                try {
                                    //Ensure the directory exists
                                    Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));

                                    //If gotten this far, path is valid. Exit from the loop
                                    break;
                                }

                                //Otherwise, shorten the path
                                catch (PathTooLongException) {
                                    //If the fix has already been attempted then nothing we can do
                                    if (attempted)
                                    {
                                        Console.WriteLine($"Failed to setup decrypted file '{Path.GetFileName(destinationPath)}' as the resulting filepath '{destinationPath}' was to long");
                                        successful = false;
                                        failed     = true;
                                        break;
                                    }

                                    //Otherwise, try to temp fix the positioning of the file
                                    else
                                    {
                                        //Construct a debug string that can be used to explain the resulting operation
                                        StringBuilder sb = new StringBuilder($"Failed to setup directory for filepath '{destinationPath}' as the filepath is to long. ");

                                        //Shorten the filename to the destination directory with additional information (To prevent copy overwrite)
                                        destinationPath = Path.Combine(DestinationPath, $"{Path.GetFileNameWithoutExtension(destinationPath)}_({Path.GetFileNameWithoutExtension(generatedFiles[i].Item1)}){Path.GetExtension(destinationPath)}");

                                        //Add the new path to the message
                                        sb.Append($"The new path of '{destinationPath}' has been assigned to try and shorten the final path");

                                        //Log the message
                                        Console.WriteLine(sb.ToString());

                                        //Flag as the fix being attempted
                                        attempted = true;
                                    }
                                }
                            }

                            //If the operation has failed, quit out
                            if (failed)
                            {
                                break;
                            }

                            //Delete the file if it already exists
                            try { File.Delete(destinationPath); } catch { }

                            //Try to move the decrypted file to the final location
                            try { File.Move(generatedFiles[i].Item1, destinationPath); } catch (Exception exec) {
                                Console.WriteLine($"Decryption operation failed to relocate decrypted file to '{destinationPath}'. ERROR: {exec.Message}");
                                successful = false;
                                break;
                            }

                            //Increment the operation percentage
                            progress += percentageUsage;
                            Console.WriteLine($"\tProgress: {(progress * 100f).ToString("F2") + '%'}");
                        }

                        //Exit from the loop if the operation was a success
                        if (successful)
                        {
                            break;
                        }
                    }
                }
            }

            //Mark the end of the operation
            return(successful);
        }
Ejemplo n.º 36
0
        private static void TestCtor(CompressionLevel level, bool? leaveOpen = null)
        {
            //Create the GZipStream
            int _bufferSize = 1024;
            var bytes = new Byte[_bufferSize];
            var baseStream = new MemoryStream(bytes, true);
            GZipStream ds;

            if (leaveOpen == null)
            {
                ds = new GZipStream(baseStream, level);
            }
            else
            {
                ds = new GZipStream(baseStream, level, leaveOpen ?? false);
            }

            //Write some data and Close the stream
            String strData = "Test Data";
            var encoding = Encoding.UTF8;
            Byte[] data = encoding.GetBytes(strData);
            ds.Write(data, 0, data.Length);
            ds.Flush();
            ds.Dispose();

            if (leaveOpen != true)
            {
                //Check that Close has really closed the underlying stream
                Assert.Throws<ObjectDisposedException>(() => { baseStream.Write(bytes, 0, bytes.Length); });
            }

            //Read the data
            Byte[] data2 = new Byte[_bufferSize];
            baseStream = new MemoryStream(bytes, false);
            ds = new GZipStream(baseStream, CompressionMode.Decompress);
            int size = ds.Read(data2, 0, _bufferSize - 5);

            //Verify the data roundtripped
            for (int i = 0; i < size + 5; i++)
            {
                if (i < data.Length)
                {
                    Assert.Equal(data[i], data2[i]);
                }
                else
                {
                    Assert.Equal(data2[i], (byte)0);
                }
            }
        }
Ejemplo n.º 37
0
        public void DoWork()
        {
            for (int i = 0; i < blocks_readahead; i++)
            {
                blocksFree.AddLast(new Block());
            }

            bool       fReading = false;
            GZipStream gstm     = null;

            for (; ;)
            {
                semaphoreToBg.WaitOne();

                Cmd cmd = null;

                lock (cmds)
                {
                    cmd = cmds.First.Value;
                    cmds.RemoveFirst();
                }

                switch (cmd.code)
                {
                case CmdCode.Exit:
                    if (gstm != null)
                    {
                        gstm.Close();
                    }
                    return;

                case CmdCode.Free:
                    blocksFree.AddLast(cmd.block);
                    break;

                case CmdCode.Seek:
                    if (offsets.Count == 0)
                    {
                        src.Seek(cmd.position, SeekOrigin.Begin);
                        currentSegment       = 0;
                        currentSegmentOffset = cmd.position;
                        cbScratch            = 0;
                    }
                    else
                    {
                        int seg    = (int)(cmd.position >> 32);
                        int offset = (int)(cmd.position & 0xffffffff);

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

                        currentSegment = (int)seg;
                        src.Seek(offsets[currentSegment], SeekOrigin.Begin);
                        currentSegmentOffset = offset;
                        cbScratch            = 0;

                        gstm = new GZipStream(src, CompressionMode.Decompress, true);

                        while (offset > 0)
                        {
                            int cb = Math.Min(offset, bufferScratch.Length);
                            cb      = gstm.Read(bufferScratch, 0, cb);
                            offset -= cb;
                        }
                    }
                    fReading = true;
                    break;

                case CmdCode.Stop:
                    fReading = false;
                    break;
                }

                if (!fReading)
                {
                    continue;
                }

                if (offsets.Count == 0)
                {
                    // this is not an archive
                    while (blocksFree.Count > 0)
                    {
                        Block b = blocksFree.First.Value;
                        blocksFree.RemoveFirst();

                        int cbBuffer = 0;
                        int cbRead   = 0;

                        if (cbScratch > 0)
                        {
                            Array.Copy(bufferScratch, b.buffer, cbScratch);
                            cbBuffer  = cbScratch;
                            cbScratch = 0;
                        }

                        cbRead = src.Read(b.buffer, cbBuffer, b.buffer.Length - cbBuffer);

                        cbBuffer += cbRead;

                        ProcessAndTransfer(b, cbBuffer);

                        if (cbRead == 0)
                        {
                            fReading = false;
                            break;
                        }
                    }
                }
                else
                {
                    // this is an archive...

                    while (blocksFree.Count > 0)
                    {
                        Block b = blocksFree.First.Value;
                        blocksFree.RemoveFirst();

                        if (gstm == null)
                        {
                            ProcessAndTransfer(b, 0);
                            fReading = false;
                            break;
                        }

                        int cbBuffer = 0;
                        int cbRead   = 0;

                        if (cbScratch > 0)
                        {
                            Array.Copy(bufferScratch, b.buffer, cbScratch);
                            cbBuffer  = cbScratch;
                            cbScratch = 0;
                        }

                        while (cbBuffer < b.buffer.Length)
                        {
                            cbRead = gstm.Read(b.buffer, cbBuffer, b.buffer.Length - cbBuffer);

                            cbBuffer += cbRead;

                            if (cbBuffer == b.buffer.Length)
                            {
                                break;
                            }

                            if (cbRead == 0)
                            {
                                break;
                            }
                        }

                        if (cbBuffer > 0)
                        {
                            ProcessAndTransfer(b, cbBuffer);
                        }
                        else
                        {
                            // put it back, since we didn't use it
                            blocksFree.AddFirst(b);
                        }

                        if (cbRead != 0)
                        {
                            continue;
                        }

                        currentSegment++;
                        currentSegmentOffset = 0;
                        if (currentSegment < offsets.Count)
                        {
                            src.Seek(offsets[currentSegment], SeekOrigin.Begin);
                            gstm.Recycle();
                        }
                        else
                        {
                            gstm.Close();
                            gstm = null;
                        }
                    }
                }
            }
        }
Ejemplo n.º 38
0
        private bool SaveFile(BBA bba, string outDir)
        {
            FileStream   f        = null;
            MemoryStream fStream  = null;
            string       FileName = bba.FileName;
            FileStream   pakFile  = m_fs;

            pakFile.Position = bba.Offset;

            int BytesToWrite = bba.UnpackedSize;
            int BytesToRead  = (bba.PackedSize % 4);

            BytesToRead = (BytesToRead == 0) ? bba.PackedSize : (4 - BytesToRead) + bba.PackedSize;
            int BytesRead = 0;

            try
            {
                f = CreateFile(outDir, FileName);
                if (f == null)
                {
                    return(false);
                }

                if (bba.UnpackedSize == 0)
                {
                    f.Close(); return(true);
                }

                fStream = new MemoryStream(BytesToRead);


                if (fStream == null)
                {
                    f.Close(); return(false);
                }

                byte[] buff = fStream.GetBuffer();


                BBAFlags Flags = (BBAFlags)bba.Flags;

                bool IsFileWriten = false;
                bool Decrypted    = false;
                bool Decompressed = false;

                BytesRead = pakFile.Read(buff, 0, BytesToRead);

                if (BytesRead == 0)
                {
                    f.Close(); return(false);
                }

                if ((Flags & BBAFlags.EncryptedHeader) == BBAFlags.EncryptedHeader)
                {
                    int    count = bba.PackedSize / bba.Unknown2 + 1;
                    byte[] b     = new byte[0x10];
                    for (int i = 0; i < count; i++)
                    {
                        Array.Copy(buff, i * bba.Unknown2, b, 0, 0x10);

                        using (MemoryStream s = new MemoryStream())
                        {
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            byte[] TmpKey = s.GetBuffer();
                            BBACrypt.DecryptFileHeader(b, b.Length, TmpKey, 0x10, false);
                        }

                        Array.Copy(b, 0, buff, i * bba.Unknown2, 0x10);
                    }
                }

                if ((Flags & BBAFlags.EncryptedEntireContent) == BBAFlags.EncryptedEntireContent)
                {
                    IsFileWriten = true;
                    int    count = BytesToRead / bba.Unknown2 + 1;
                    byte[] b     = new byte[0x8000];

                    for (int i = 0; i < count; i++)
                    {
                        int ToDecrypt = ((BytesToRead - (i * bba.Unknown2 + b.Length)) >= 0) ? b.Length : BytesToRead - i * bba.Unknown2;

                        Array.Copy(buff, i * bba.Unknown2, b, 0, ToDecrypt);

                        using (MemoryStream s = new MemoryStream())
                        {
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                            byte[] TmpKey = s.GetBuffer();
                            bool   bRes   = BBACrypt.DecryptIndexTable(b, ToDecrypt, TmpKey, 0x10);
                            if (!bRes)
                            {
                                this.listBox1.Items.Add("Failed to decrypt: " + bba.FileName);
                            }

                            int ToWrite = (ToDecrypt == 0x8000) ? 0x8000 : BytesToWrite - i * bba.Unknown2;
                            f.Write(b, 0, ToWrite);
                        }
                    }
                }


                if ((Flags & BBAFlags.Gziped) == BBAFlags.Gziped &&
                    !Decompressed)
                {
                    IsFileWriten = true;
                    using (MemoryStream m = new MemoryStream(buff))
                    {
                        using (GZipStream Decompress = new GZipStream(m, CompressionMode.Decompress))
                        {
                            MemoryStream DecompressionStream = new MemoryStream(0x10000);
                            byte[]       DecompressionBuffer = DecompressionStream.GetBuffer();
                            int          numRead;
                            while ((numRead = Decompress.Read(DecompressionBuffer, 0, DecompressionBuffer.Length)) != 0)
                            {
                                f.Write(DecompressionBuffer, 0, numRead);
                                BytesToWrite -= numRead;
                            }
                            DecompressionStream.Dispose();
                            DecompressionStream.Close();
                            Decompressed = true;
                        }
                    }
                }


                if (!IsFileWriten)
                {
                    f.Write(buff, 0, bba.PackedSize);
                }


                //                         MemoryStream s = new MemoryStream();
                //
                //                             s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                //                             s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                //                             s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                //                             s.Write(BitConverter.GetBytes(bba.PathSize), 0, 4);
                //
                //                             byte[] TmpKey = s.GetBuffer();
                //                             buff = new byte[bba.UnpackedSize];
                //                             pakFile.Read(buff, 0, buff.Length);
                //
                //                             bool bRes = BBACrypt.DecryptIndexTable(buff, buff.Length, TmpKey, 0x10);
                //
                //                             if (bRes == false)
                //                             {
                //                                 this.listBox1.SelectedItem = listBox1.Items.Add("Failed to decrypt: " + bba.FileName );
                //                             }
                //                             else
                //                             {
                //
                //                             }
                //                      }
            }
            catch (System.Exception ex)
            {
                if (FileName != null)
                {
                    this.listBox1.Items.Add(FileName);
                }

                this.listBox1.Items.Add(ex.Message);
            }

            f.Position = 0;

            int crc32 = Compression.GetStreamCRC32(f);

            if (bba.UnpackedCRC32 != crc32)
            {
                listBox1.Items.Add("CRC mismatch: " + bba.FileName);
            }

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

            if (fStream != null)
            {
                fStream.Dispose();
                fStream.Close();
            }



            DateTime time = DateTime.FromFileTime(bba.Time);

            File.SetCreationTime(outDir + @"\" + FileName, time);
            File.SetLastWriteTime(outDir + @"\" + FileName, time);
            File.SetLastAccessTime(outDir + @"\" + FileName, time);

            return(true);
        }
Ejemplo n.º 39
0
        public void UnPack(string file, bool SaveToDisk)
        {
            Files         = null;
            Signature     = Farc.FArC;
            FT            = false;
            Console.Title = "PD_Tool: FARC Extractor - Archive: " + Path.GetFileName(file);
            if (File.Exists(file))
            {
                KKtIO  reader    = KKtIO.OpenReader(file);
                string directory = Path.GetFullPath(file).Replace(Path.GetExtension(file), "");
                Signature = (Farc)reader.ReadInt32Endian(true);
                if (Signature == Farc.FARC)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLenght = reader.ReadInt32Endian(true);
                    int Mode         = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();
                    bool GZip = (Mode & 2) == 2;
                    bool ECB  = (Mode & 4) == 4;

                    int FARCType = reader.ReadInt32Endian(true);
                    FT = FARCType == 0x10;
                    bool CBC = !FT && FARCType != 0x40;
                    if (ECB && CBC)
                    {
                        byte[] Header = new byte[HeaderLenght - 0x08];
                        FT = true;
                        reader.Close();
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(0x10, 0);

                        using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                            GetAes(true, null).CreateDecryptor(), CryptoStreamMode.Read))
                            cryptoStream.Read(Header, 0x00, HeaderLenght - 0x08);
                        Header = SkipData(Header, 0x10);
                        KKtIO CBCreader = new KKtIO(new MemoryStream(Header));
                        CBCreader.BaseStream.Seek(0, 0);

                        FARCType = CBCreader.ReadInt32Endian(true);
                        FT       = FARCType == 0x10;
                        if (CBCreader.ReadInt32Endian(true) == 1)
                        {
                            Files = new FARCFile[CBCreader.ReadInt32Endian(true)];
                        }
                        CBCreader.ReadUInt32();
                        HeaderReader(HeaderLenght, ref Files, ref CBCreader);
                        CBCreader.Close();
                    }
                    else
                    {
                        if (reader.ReadInt32Endian(true) == 1)
                        {
                            Files = new FARCFile[reader.ReadInt32Endian(true)];
                        }
                        reader.ReadUInt32();
                        HeaderReader(HeaderLenght, ref Files, ref reader);
                        reader.Close();
                    }

                    for (int i = 0; i < Files.Length; i++)
                    {
                        int FileSize = ECB || Files[i].ECB ? (int)Main.
                                       Align(Files[i].SizeComp, 0x10) : Files[i].SizeComp;
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[FileSize];

                        bool Encrypted = false;
                        if (ECB)
                        {
                            if ((FT && Files[i].ECB) || CBC)
                            {
                                using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                                    GetAes(true, null).CreateDecryptor(), CryptoStreamMode.Read))
                                    cryptoStream.Read(Files[i].Data, 0, FileSize);
                                Files[i].Data = SkipData(Files[i].Data, 0x10);
                            }
                            else
                            {
                                using (CryptoStream cryptoStream = new CryptoStream(stream,
                                                                                    GetAes(false, null).CreateDecryptor(), CryptoStreamMode.Read))
                                    cryptoStream.Read(Files[i].Data, 0, FileSize);
                            }
                            Encrypted = true;
                        }

                        bool Compressed = false;
                        bool LocalGZip  = (FT && Files[i].GZip) || GZip && Files[i].SizeUnc != 0;
                        if (LocalGZip)
                        {
                            GZipStream gZipStream;
                            if (Encrypted)
                            {
                                gZipStream = new GZipStream(new MemoryStream(
                                                                Files[i].Data), CompressionMode.Decompress);
                                stream.Close();
                            }
                            else
                            {
                                gZipStream = new GZipStream(stream, CompressionMode.Decompress);
                            }
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            gZipStream.Read(Files[i].Data, 0, Files[i].SizeUnc);

                            Compressed = true;
                        }

                        if (!Encrypted && !Compressed)
                        {
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            stream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                            stream.Close();
                        }

                        if (SaveToDisk)
                        {
                            KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                            writer.Write(Files[i].Data);
                            writer.Close();
                            Files[i].Data = null;
                        }
                    }
                }
                else if (Signature == Farc.FArC)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLength = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();
                    HeaderReader(HeaderLength, ref Files, ref reader);
                    reader.Close();

                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[Files[i].SizeComp];
                        stream.Read(Files[i].Data, 0, Files[i].SizeComp);
                        stream.Close();

                        using (MemoryStream memorystream = new MemoryStream(Files[i].Data))
                        {
                            GZipStream gZipStream = new GZipStream(memorystream, CompressionMode.Decompress);
                            Files[i].Data = new byte[Files[i].SizeUnc];
                            gZipStream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                        }

                        KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                        writer.Write(Files[i].Data);
                        writer.Close();
                        Files[i].Data = null;
                    }
                }
                else if (Signature == Farc.FArc)
                {
                    Directory.CreateDirectory(directory);
                    int HeaderLength = reader.ReadInt32Endian(true);
                    reader.ReadUInt32();

                    HeaderReader(HeaderLength, ref Files, ref reader);
                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileStream stream = new FileStream(file, FileMode.Open,
                                                           FileAccess.ReadWrite, FileShare.ReadWrite);
                        stream.Seek(Files[i].Offset, 0);
                        Files[i].Data = new byte[Files[i].SizeUnc];
                        stream.Read(Files[i].Data, 0, Files[i].SizeUnc);
                        stream.Close();

                        KKtIO writer = KKtIO.OpenWriter(Path.Combine(directory, Files[i].Name), true);
                        writer.Write(Files[i].Data);
                        writer.Close();
                        Files[i].Data = null;
                    }
                }
                else
                {
                    Console.WriteLine("Unknown signature");
                    reader.Close();
                }
            }
            else
            {
                Console.WriteLine("File {0} doesn't exist.", Path.GetFileName(file));
            }
            Console.Clear();
            Console.Title = "PD_Tool: FARC Extractor";
        }
Ejemplo n.º 40
0
        /// <summary>
        /// Restore file created in blob storage by BackupAzureTables to the destination table name specified.
        /// </summary>
        /// <param name="DestinationTableName">Name of the Azure Table to restore to -  may be different than name backed up originally.</param>
        /// <param name="OriginalTableName">Name of the Azure Table originally backed (required for determining blob directory to use)</param>
        /// <param name="BlobRoot">Name to use as blob root folder.</param>
        /// <param name="WorkingDirectory">Local directory (path) with authority to create/write a file.</param>
        /// <param name="BlobFileName">Name of the blob file to restore.</param>
        /// <param name="TimeoutSeconds">Set timeout for table client.</param>
        /// <returns>A string indicating the table restored and record count.</returns>
        public string RestoreTableFromBlob(string DestinationTableName, string OriginalTableName, string BlobRoot, string WorkingDirectory, string BlobFileName, int TimeoutSeconds = 30)
        {
            string result = "Error";

            if (String.IsNullOrWhiteSpace(DestinationTableName))
            {
                throw new ParameterSpecException("DestinationTableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(OriginalTableName))
            {
                throw new ParameterSpecException("OriginalTableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(WorkingDirectory))
            {
                throw new ParameterSpecException("WorkingDirectory is missing.");
            }

            if (String.IsNullOrWhiteSpace(BlobFileName))
            {
                throw new ParameterSpecException(String.Format("Invalid BlobFileName '{0}' specified.", BlobFileName));
            }
            bool Decompress = BlobFileName.EndsWith(".7z");

            if (String.IsNullOrWhiteSpace(BlobRoot))
            {
                throw new ParameterSpecException(String.Format("Invalid BlobRoot '{0}' specified.", BlobRoot));
            }

            if (Path.GetFullPath(WorkingDirectory) != WorkingDirectory)
            {
                throw new ParameterSpecException(String.Format("Invalid WorkingDirectory '{0}' specified.", WorkingDirectory));
            }

            if (!AZStorage.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureBlobConnectionSpec).Password, out AZStorage.CloudStorageAccount StorageAccountAZ))
            {
                throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
            }

            try
            {
                AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ);
                var container = ClientBlob.GetContainerReference(BlobRoot);
                container.CreateIfNotExists();
                AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobRoot.ToLower() + "-table-" + OriginalTableName.ToLower());

                string WorkingFileNamePath           = Path.Combine(WorkingDirectory, BlobFileName);
                string WorkingFileNamePathCompressed = Path.Combine(WorkingDirectory, BlobFileName);

                /*
                 * If file is compressed, WorkingFileNamePath will be set to .txt
                 * If file is not compressed WorkingFileNamePathCompressed will be left as .txt
                 */
                if (Decompress)
                {
                    WorkingFileNamePath = WorkingFileNamePath.Replace(".7z", ".txt");
                }
                else
                {
                    //WorkingFileNamePathCompressed = WorkingFileNamePathCompressed.Replace(".txt", ".7z");
                }

                AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(BlobFileName);
                BlobBlock.DownloadToFile(WorkingFileNamePathCompressed, FileMode.Create);

                //https://www.tutorialspoint.com/compressing-and-decompressing-files-using-gzip-format-in-chash
                if (Decompress)
                {
                    FileStream FileToDeCompress = File.OpenRead(WorkingFileNamePathCompressed);
                    using (FileStream OutFileDecompressed = new FileStream(WorkingFileNamePath, FileMode.Create))
                    {
                        using (var zip = new GZipStream(FileToDeCompress, CompressionMode.Decompress, true))
                        {
                            byte[] buffer = new byte[FileToDeCompress.Length];
                            while (true)
                            {
                                int count = zip.Read(buffer, 0, buffer.Length);
                                if (count != 0)
                                {
                                    OutFileDecompressed.Write(buffer, 0, count);
                                }
                                if (count != buffer.Length)
                                {
                                    break;
                                }
                            }
                        }
                        FileToDeCompress.Close();
                        OutFileDecompressed.Close();
                    }
                }

                result = RestoreTableFromFile(DestinationTableName, WorkingFileNamePath, TimeoutSeconds);
                // Cleanup files
                if (File.Exists(WorkingFileNamePath))
                {
                    File.Delete(WorkingFileNamePath);
                }
                if (File.Exists(WorkingFileNamePathCompressed))
                {
                    File.Delete(WorkingFileNamePathCompressed);
                }
            }
            catch (ConnectionException cex)
            {
                throw cex;
            }
            catch (Exception ex)
            {
                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
            }
            finally
            {
            }
            return(result);
        }
Ejemplo n.º 41
0
        public bool LoadDocument(string documentFilePath)
        {
            try
            {
                this.UnloadDocument();

                if (textEditor == null || string.IsNullOrWhiteSpace(documentFilePath) ||
                    File.Exists(documentFilePath) == false)
                {
                    return(false);
                }
                string fileExt = IoPath.GetExtension(documentFilePath);
                if (string.IsNullOrWhiteSpace(fileExt))
                {
                    return(false);
                }
                if (!string.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase) &&
                    !string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }

                if (string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
                {
                    using (FileStream fileStream = File.OpenRead(documentFilePath))
                    {
                        using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Decompress))
                        {
                            // Text Editor does not work with this stream, so we read the data to memory stream...
                            MemoryStream memoryStream = new MemoryStream();
                            // Use this method is used to read all bytes from a stream.
                            int    totalCount = 0;
                            int    bufferSize = 512;
                            byte[] buffer     = new byte[bufferSize];
                            while (true)
                            {
                                int bytesRead = zipStream.Read(buffer, 0, bufferSize);
                                if (bytesRead == 0)
                                {
                                    break;
                                }

                                memoryStream.Write(buffer, 0, bytesRead);
                                totalCount += bytesRead;
                            }

                            if (totalCount > 0)
                            {
                                memoryStream.Position = 0;
                            }

                            textEditor.Load(memoryStream);

                            memoryStream.Close();
                        }
                    }
                }
                else
                {
                    textEditor.Load(documentFilePath);
                }

                this.UpdateFoldings();
            }
            catch (Exception ex)
            {
                this.ReportError(ex);

                return(false);
            }

            if (string.Equals(documentFilePath, _svgFilePath, StringComparison.OrdinalIgnoreCase))
            {
                return(this.ConvertDocument());
            }

            if (this.SaveDocument() && File.Exists(_svgFilePath))
            {
                return(this.ConvertDocument(documentFilePath));
            }

            return(true);
        }
        static void Main(string[] args)
        {
            var files = new List <string>();

            Console.WriteLine("Add source path: ");
            var sourcePath = Console.ReadLine();                          // ../../text.txt
            var resultPath = "../../result/";                             // where result will be stored.

            Directory.CreateDirectory(Path.GetDirectoryName(resultPath)); // create directory for result.

            Console.WriteLine("Add number of parts: ");
            var parts = int.Parse(Console.ReadLine()); //user add number of parts.

            //Slice
            using (var readSource = new FileStream(sourcePath, FileMode.Open))
            {
                var sizeOfSource = readSource.Length;
                var eachPartSize = (readSource.Length / parts) + readSource.Length % parts; //take each part size.

                for (int i = 1; i <= parts; i++)
                {
                    //create name for each new part with number and extension.
                    using (var result = new FileStream(resultPath + Path.GetFileNameWithoutExtension(sourcePath) + "-" + i + ".gz", FileMode.Create))
                    {
                        using (var compressSteam = new GZipStream(result, CompressionMode.Compress, false))
                        {
                            //add each new file part to list files.
                            files.Add(resultPath + Path.GetFileNameWithoutExtension(sourcePath) + "-" + i + ".gz");

                            var num = 0;
                            while (true)
                            {
                                num++;
                                var buffer    = new byte[eachPartSize];
                                var readBytes = readSource.Read(buffer, 0, buffer.Length);
                                compressSteam.Write(buffer, 0, readBytes);

                                if (num == 1)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }


            //Assembly
            using (var result = new FileStream(resultPath + Path.GetFileName(sourcePath), FileMode.Create))
            {
                for (int i = 0; i < files.Count; i++)
                {
                    using (var source = new FileStream(files[i], FileMode.Open))
                    {
                        using (var decompressStream = new GZipStream(source, CompressionMode.Decompress, false))
                        {
                            var buffer = new byte[4096];
                            while (true)
                            {
                                var readBytes = decompressStream.Read(buffer, 0, buffer.Length); // read compress files.
                                if (readBytes == 0)
                                {
                                    break;
                                }

                                result.Write(buffer, 0, readBytes); // Writes assembly file to result directory.
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 43
0
        static void Main(string[] args)
        {
            {
                if (args.Length == 1)
                {
                    Console.WriteLine(" /c to compress");
                    Console.WriteLine(" /d to decompress");
                    Console.WriteLine(" /i={filepath} inputfile");
                    Console.WriteLine(" /o={filepath} outputfile");
                }
                string infile    = "";
                string outfile   = "";
                string operation = "";

                int procCount = Environment.ProcessorCount;
                var threads   = new List <Thread>(procCount);
                if (args.Length > 1)
                {
                    if (args.Contains("/c"))
                    {
                        operation = "compress";
                    }
                    if (args.Contains("/d"))
                    {
                        operation = "decompress";
                    }
                    args.ToList().ForEach(x =>
                    {
                        if (x.Contains("/i="))
                        {
                            infile = x.Replace("/i=", "");
                        }
                        if (x.Contains("/o="))
                        {
                            outfile = x.Replace("/o=", "");
                        }
                    });
                    Console.WriteLine("Choosed operation: " + operation);
                    logger.Trace("Choosed operation: " + operation);
                    if (infile != "")
                    {
                        if (outfile != "")
                        {
                            Console.WriteLine("Source file: " + infile);
                            logger.Trace("Source file: " + infile);
                            Console.WriteLine("Output file: " + outfile);
                            logger.Trace("Output file: " + outfile);
                            if (outfile != infile)
                            {
                                if (operation == "compress")
                                {
                                    try
                                    {
                                        using (FileStream fsin = new FileStream(infile, FileMode.Open, FileAccess.Read, FileShare.Read))
                                            using (FileStream fsout = new FileStream(outfile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                                                using (GZipStream gz = new GZipStream(fsout, CompressionMode.Compress, false))
                                                {
                                                    var blockCount = fsin.Length / blockLength + 1;
                                                    var range      = Math.Ceiling((double)blockCount / procCount);
                                                    for (var p = 0; p < procCount; p++)
                                                    {
                                                        var start = Convert.ToInt32(p * range);
                                                        var stop  = Convert.ToInt32(start + range);

                                                        threads.Add(new Thread(() =>
                                                        {
                                                            using (FileStream fsinput = new FileStream(infile, FileMode.Open, FileAccess.Read,
                                                                                                       FileShare.Read))
                                                            {
                                                                var len = 0;
                                                                for (var i = start; i < stop; i++)
                                                                {
                                                                    lock (sync)
                                                                    {
                                                                        var buf = new byte[blockLength];
                                                                        len     = fsinput.Read(buf, 0, blockLength);
                                                                        gz.Write(buf, 0, len);
                                                                    }
                                                                    if (len < blockLength)
                                                                    {
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                        }));
                                                    }
                                                    threads.ForEach(x => x.Start());
                                                    threads.ForEach(x => x.Join());
                                                }
                                        Console.WriteLine("Done");
                                        logger.Trace("Done");
                                        Console.ReadKey();
                                        return;
                                    }
                                    catch (Exception ee)
                                    {
                                        Handler.ErrorHandler(ee, logger);
                                        return;
                                    }
                                }
                                if (operation == "decompress")
                                {
                                    try
                                    {
                                        using (FileStream fsin = new FileStream(infile, FileMode.Open, FileAccess.Read, FileShare.Read))
                                            using (FileStream fsout = new FileStream(outfile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                                                using (GZipStream gz = new GZipStream(fsin, CompressionMode.Decompress, false))
                                                {
                                                    for (var p = 0; p < procCount; p++)
                                                    {
                                                        threads.Add(new Thread(() =>
                                                        {
                                                            try
                                                            {
                                                                var len = 0;
                                                                do
                                                                {
                                                                    lock (sync)
                                                                    {
                                                                        var buf = new byte[blockLength];
                                                                        len     = gz.Read(buf, 0, blockLength);
                                                                        fsout.Write(buf, 0, len);
                                                                    }
                                                                } while (len >= 1024);
                                                            }
                                                            catch (Exception ee)
                                                            {
                                                                Handler.ErrorHandler(ee, logger);
                                                            }
                                                        }));
                                                    }
                                                    threads.ForEach(x => x.Start());
                                                    threads.ForEach(x => x.Join());
                                                }
                                        Console.WriteLine("Done");
                                        logger.Trace("Done");
                                        Console.ReadKey();
                                    }
                                    catch (Exception ee)
                                    {
                                        Handler.ErrorHandler(ee, logger);
                                    }
                                }
                            }
                            else
                            {
                                Handler.WarningHandler("Input and Output files have the same names", logger);
                            }
                        }
                        else
                        {
                            Handler.WarningHandler("Output file name not specified!", logger);
                        }
                    }
                    else
                    {
                        Handler.WarningHandler("Input file name not specified", logger);
                    }
                }
            }
        }
Ejemplo n.º 44
0
        public void DecompressFile(string sourceFile, string destinationFile)
        {
            // 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)
            {
                MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // Make sure we allways close all streams
                if (sourceStream != null)
                {
                    sourceStream.Close( );
                }

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

                if (destinationStream != null)
                {
                    destinationStream.Close( );
                }
            }
        }
Ejemplo n.º 45
0
        private static string ReadXml(ref String gameCode, ref int oldCount, ref int newCount, ref DateTime end)
        {
            string link    = string.Empty;
            int    counter = 0;

            oldCount = 0;
            newCount = 0;
            do
            {
                string oldCode = gameCode;
                if (counter++ > 10000)
                {
                    try
                    {
                        mainDriver.Navigate().Refresh();
                    }
                    catch (Exception)
                    {
                        mainDriver = StartDriver("https://hsreplay.net/", "Full speed", true);
                    }
                    new WebDriverWait(mainDriver, TimeSpan.FromSeconds(10))
                    .Until(ExpectedConditions.ElementExists(By.LinkText("Full speed")))
                    .Click();
                    Console.WriteLine("Refresh");
                    counter = 0;
                }
                try
                {
                    var elements = mainDriver.FindElementsByClassName("replay-feed-item");
                    link     = elements[elements.Count - 1].GetAttribute("href");
                    gameCode = link.Split('/')[4];
                    if (gameCode == oldCode)
                    {
                        Console.Write("o");
                        oldCount++;
                        continue;
                    }
                }
                catch (Exception)
                {
                    Console.Write("e");
                    continue;
                }
                Console.Write(".");
                newCount++;
            } while (gameCode == "" || Game.ContainsGame(gameCode));

            end = DateTime.Now;
            Console.WriteLine();
            replayDriver = StartDriver(link, "Download Replay XML", false);
            if (replayDriver == null)
            {
                return("");
            }
            IWebElement xmlLink = new WebDriverWait(replayDriver, TimeSpan.FromSeconds(100))
                                  .Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Download Replay XML")));
            string href = xmlLink.GetAttribute("href");

            replayDriver.Url = "about:blank";

            Stream input;

            try
            {
                input = WebRequest.Create(href).GetResponse().GetResponseStream();
            }
            catch (WebException)
            {
                return(null);
            }
            StringBuilder sb = new StringBuilder();

            using (var zip = new GZipStream(input, CompressionMode.Decompress, true))
            {
                byte[] b = new byte[1048576];
                while (true)
                {
                    int count = zip.Read(b, 0, b.Length);
                    if (count != 0)
                    {
                        sb.Append(Encoding.Default.GetString(b));
                    }
                    if (count != b.Length)
                    {
                        break;
                    }
                }
            }
            input.Close();
            return(sb.ToString());
        }
        //
        // Fix a compressed jsgz file, decompresses and recompress accordingly
        //
        static void FixJSGZFile(string jsgzPath, bool isRelease)
        {
            EditorUtility.DisplayProgressBar(ProgressTitle, "Uncompressing jsgz...", 0.0f);

            string buildJSFolder = Path.GetDirectoryName(jsgzPath);
            string buildJSName   = Path.GetFileNameWithoutExtension(jsgzPath);

            // create buffer for decompressing jsgz
            const int size = 134217728;             //128MB should be more than enough :/

            byte[] buffer    = new byte[size];
            int    readcount = 0;

            // open jsgz file
            using (FileStream inputFileStream = new FileStream(jsgzPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // create demcompress stream from opened jsgz file
                using (GZipStream decompressionStream = new GZipStream(inputFileStream, CompressionMode.Decompress))
                {
                    // read decompressed buffer
                    readcount = decompressionStream.Read(buffer, 0, size);
                    inputFileStream.Close();

                    if (readcount <= 0)
                    {
                        UnityEngine.Debug.LogError("WebGLRetinaTools: Failed to read from jsgz file can't continue.");
                        return;
                    }
                }
            }

            // create a string builder to edit from the decompressed buffer
            string        decompressedSourceStr = System.Text.Encoding.UTF8.GetString(buffer, 0, readcount);
            StringBuilder source = new StringBuilder(decompressedSourceStr);

            EditorUtility.DisplayProgressBar(ProgressTitle, "Fixing jsgz source...", 0.5f);

            // fix the source
            if (isRelease)
            {
                FixJSFileContentsRelease(ref source);
            }
            else
            {
                FixJSFileContentsDevelopment(ref source);
            }

            EditorUtility.DisplayProgressBar(ProgressTitle, "Recompressing jsgz...", 1.0f);

            // write the edited text to file
            string editedSourcePath = Path.Combine(buildJSFolder, buildJSName + JsExt);

            File.WriteAllText(editedSourcePath, source.ToString(), Encoding.UTF8);

            // compress the edited file
            using (FileStream editedSourceStream = File.OpenRead(editedSourcePath))
            {
                using (FileStream compressOutputStream = File.Create(jsgzPath))
                {
                    buffer = new byte[editedSourceStream.Length];
                    editedSourceStream.Read(buffer, 0, buffer.Length);

                    using (GZipStream output = new GZipStream(compressOutputStream, CompressionMode.Compress))
                    {
                        output.Write(buffer, 0, buffer.Length);
                    }

                    compressOutputStream.Close();
                }
                editedSourceStream.Close();
            }
            // delete the temp edited file
            File.Delete(editedSourcePath);
        }
Ejemplo n.º 47
0
        public bool LoadMCSharp()
        {
            bool       success = false;
            FileStream fs      = File.OpenRead(mFileName);

            try
            {
                // Open a GZIP Stream
                GZipStream gs  = new GZipStream(fs, CompressionMode.Decompress);
                byte[]     ver = new byte[2];
                gs.Read(ver, 0, ver.Length);
                ushort version = BitConverter.ToUInt16(ver, 0);

                if (version == 1900)
                {
                }
                else if (version == 1874)
                {
                    // Read in the header
                    byte[] header = new byte[16];
                    gs.Read(header, 0, header.Length);

                    // Map Dimensions
                    x = BitConverter.ToUInt16(header, 0);
                    y = BitConverter.ToUInt16(header, 2);
                    z = BitConverter.ToUInt16(header, 4);
                    //
                    spawnx             = BitConverter.ToUInt16(header, 6);
                    spawnz             = BitConverter.ToUInt16(header, 8);
                    spawny             = BitConverter.ToUInt16(header, 10);
                    spawnrotx          = header[12];
                    spawnroty          = header[13];
                    lvlVisitPermission = header[14];
                    lvlBuildPermission = header[15];

                    // Read in block data
                    blocks = new byte[x * y * z];
                    gs.Read(blocks, 0, blocks.Length);
                }
                else
                {
                    // Read in the header
                    byte[] header = new byte[12];
                    gs.Read(header, 0, header.Length);

                    // Map Dimensions
                    x = version;
                    y = BitConverter.ToUInt16(header, 0);
                    z = BitConverter.ToUInt16(header, 2);

                    // Spawn
                    spawnx    = BitConverter.ToUInt16(header, 4);
                    spawnz    = BitConverter.ToUInt16(header, 6);
                    spawny    = BitConverter.ToUInt16(header, 8);
                    spawnrotx = header[10];
                    spawnroty = header[11];

                    // Read in block data
                    blocks = new byte[x * y * z];
                    gs.Read(blocks, 0, blocks.Length);
                }

                // Close the GZIP stream
                gs.Close();
                success      = true;
                mInputFormat = LevelFormat.MCSharp;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error while loading the old MCSharp level.");
                Console.WriteLine(e.Message);
            }
            return(success);
        }
Ejemplo n.º 48
0
        string GetHttpResponse(HttpWebResponse res)
        {
            const int WorkingBufferSize = 1024;
            string requestTokenResponse = string.Empty;
            string contentEncoding = string.Empty;

            using (var respStream = res.GetResponseStream())
            {
#if !SILVERLIGHT || WINDOWS_PHONE
                contentEncoding = res.Headers["Content-Encoding"] ?? "";
#endif
                if (contentEncoding.ToLower().Contains("gzip"))
                {
                    using (var gzip = new GZipStream(respStream, CompressionMode.Decompress))
                    {
                        using (var memStr = new MemoryStream())
                        {
                            byte[] buffer = new byte[WorkingBufferSize];
                            int n;
                            while ((n = gzip.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                memStr.Write(buffer, 0, n);
                            }
                            memStr.Position = 0;
                            using (var strmRdr = new StreamReader(memStr))
                            {
                                requestTokenResponse = strmRdr.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (var respReader = new StreamReader(respStream))
                    {
                        requestTokenResponse = respReader.ReadToEnd();
                    }
                }
            }

            return requestTokenResponse;
        }
Ejemplo n.º 49
0
        /// <summary>
        /// 将从网络中接收过来的数据流解压缩,自动识别数据流流的标头,
        /// 若标头含有表示符号”MustDecompressed“和其长度则需要将
        /// 接收到的数据流进行解压处理,若标头并不含有压缩识别符号,
        /// 则不进行压缩处理。
        /// </summary>
        /// <param name="inputStream"></param>
        /// <param name="outputStream"></param>
        /// <param name="emptyBuffer"></param>
        protected override void DecompressStream(Stream inputStream, Stream outputStream, byte[] emptyBuffer)
        {
            /* 输入流有几种类型,有些是不能够定位的,如来自网络的数据流,
             *
             * 有些是可以定位的如内存中的数据流,对于那些不能够重定位的
             * 数据流,先将所有数据读入内存流后再做处理。
             *
             * */
            Stream orgInputStream = null;

            try
            {
                if (inputStream.CanSeek)
                {
                    orgInputStream = inputStream;
                }
                else
                {
                    orgInputStream = new MemoryStream();
                    byte[] buffer = new byte[BufferSize];
                    InternalNormalProcessStream(inputStream, orgInputStream, buffer);
                    orgInputStream.Position = 0;
                }

                byte[] bIndicatorSize = new byte[4];
                int    nrd            = orgInputStream.Read(bIndicatorSize, 0, bIndicatorSize.Length);
                if (nrd != bIndicatorSize.Length)
                {
                    throw new ApplicationException("Unrecognized input stream format");
                }
                int indicatorSize = BitConverter.ToInt32(bIndicatorSize, 0);

                // 识别出数据流压缩标记长度。
                if (indicatorSize == CompressedIndicator.Length)
                {
                    byte[] bIndicator = new byte[indicatorSize];
                    if (orgInputStream.Read(bIndicator, 0, indicatorSize) != indicatorSize)
                    {
                        throw new ApplicationException("Unrecognized stream format");
                    }
                    string indicator = Encoding.ASCII.GetString(bIndicator);

                    // 识别出数据流压缩标记。
                    if (indicator.Equals(CompressedIndicator, StringComparison.Ordinal))
                    {
                        int rdbytes = -1;
                        using (GZipStream gzips = new GZipStream(orgInputStream, CompressionMode.Decompress, true))
                        {
                            while ((rdbytes = gzips.Read(emptyBuffer, 0, emptyBuffer.Length)) > 0)
                            {
                                outputStream.Write(emptyBuffer, 0, rdbytes);
                            }
                        }

                        // 压缩处理完毕,此时必须返回。
                        return;
                    }
                }

                orgInputStream.Position = 0;
                InternalNormalProcessStream(orgInputStream, outputStream, emptyBuffer);
            }
            finally
            {
                if (orgInputStream != null)
                {
                    orgInputStream.Close();
                }
            }
        }
Ejemplo n.º 50
0
        public void Decompress_Canterbury(int innerIterations, string fileName, CompressionLevel compressLevel)
        {
            string zipFilePath = Path.Combine("GZTestData", "Canterbury", "GZcompressed", fileName + ".gz");
            string sourceFilePath = Path.Combine("GZTestData", "Canterbury", fileName);
            byte[] outputRead = new byte[new FileInfo(sourceFilePath).Length];
            MemoryStream[] memories = new MemoryStream[innerIterations];
            GZipStream[] gzips = new GZipStream[innerIterations];
            foreach (var iteration in Benchmark.Iterations)
            {
                for (int i = 0; i < innerIterations; i++)
                    memories[i] = new MemoryStream(File.ReadAllBytes(zipFilePath));

                using (iteration.StartMeasurement())
                    for (int i = 0; i < innerIterations; i++)
                        using (GZipStream unzip = new GZipStream(memories[i], CompressionMode.Decompress))
                            unzip.Read(outputRead, 0, outputRead.Length);

                for (int i = 0; i < innerIterations; i++)
                    memories[i].Dispose();
            }
        }
Ejemplo n.º 51
0
        public static void Decompress(string inFile, string outFile)
        {
            DateTime start = DateTime.Now;

            Thread[] threadPool;
            int      threadsNum = Environment.ProcessorCount;

            byte[][] dataArray  = new byte[threadsNum][];
            byte[][] zDataArray = new byte[threadsNum][];
            int      partSize;
            int      zPartSize;
            int      offsetMark = 4;
            int      dataLength = offsetMark * 2;

            byte[] buffer = new byte[dataLength];
            try
            {
                using (FileStream inStream = new FileStream(inFile, FileMode.Open))
                {
                    using (FileStream outStream = File.Create(outFile))
                    {
                        Console.WriteLine("Decompressing... Please wait...");
                        while (inStream.Position < inStream.Length)
                        {
                            threadPool = new Thread[threadsNum];
                            for (int partCounter = 0; partCounter < threadsNum; partCounter++)
                            {
                                if (inStream.Position < inStream.Length)
                                {
                                    inStream.Read(buffer, 0, dataLength);
                                    zPartSize = BitConverter.ToInt32(buffer, offsetMark);
                                    zDataArray[partCounter] = new byte[zPartSize];
                                    buffer.CopyTo(zDataArray[partCounter], 0);
                                    inStream.Read(zDataArray[partCounter], dataLength, zPartSize - dataLength);
                                    partSize = BitConverter.ToInt32(zDataArray[partCounter], zPartSize - offsetMark);
                                    dataArray[partCounter]  = new byte[partSize];
                                    threadPool[partCounter] = new Thread(DecompressMachine);
                                    threadPool[partCounter].Start(partCounter);
                                }
                            }
                            for (int partCounter = 0; partCounter < threadsNum; partCounter++)
                            {
                                if (threadPool[partCounter] != null)
                                {
                                    threadPool[partCounter].Join();
                                    outStream.Write(dataArray[partCounter], 0, dataArray[partCounter].Length);
                                }
                            }
                        }
                        void DecompressMachine(object obj)
                        {
                            int thread = (int)obj;

                            using (MemoryStream mStream = new MemoryStream(zDataArray[thread]))
                            {
                                using (GZipStream unzipStream = new GZipStream(mStream, CompressionMode.Decompress))
                                {
                                    unzipStream.Read(dataArray[thread], 0, dataArray[thread].Length);
                                }
                            }
                            Console.Write("|");
                        }
                        DateTime end = DateTime.Now;
                        Console.WriteLine("\nFile {0} is decompressed!", inFile);
                        Console.WriteLine("Passed time: {0}", end - start);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 52
0
 /// <summary>
 /// Decompress the given byte array
 /// </summary>
 /// <param name="gzip">byte array to decompress</param>
 /// <returns>decompressed byte array</returns>
 public static byte[] Decompress(byte[] gzip)
 {
     // Create a GZIP stream with decompression mode.
     // ... Then create a buffer and write into while reading from the GZIP stream.
     using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
     {
         const int size = 4096;
         byte[] buffer = new byte[size];
         using (MemoryStream memory = new MemoryStream())
         {
             int count = 0;
             do
             {
                 count = stream.Read(buffer, 0, size);
                 if (count > 0)
                 {
                     memory.Write(buffer, 0, count);
                 }
             }
             while (count > 0);
             return memory.ToArray();
         }
     }
 }
Ejemplo n.º 53
0
 private CompressionBlock Decompress(CompressionBlock block)
 {
     byte[] buffer = new byte[_blockLength];
     using (MemoryStream ms = new MemoryStream(block.Data))
     {
         using (GZipStream gz = new GZipStream(ms, CompressionMode.Decompress)) block.Data = buffer.Take(0, gz.Read(buffer, 0, buffer.Length));
     }
     return(block);
 }
Ejemplo n.º 54
0
        /// <summary>
        ///   Gunzips the specified input.
        /// </summary>
        /// <param name="input"> The input. </param>
        /// <returns> </returns>
        /// <remarks>
        /// </remarks>
        public static string Gunzip(this byte[] input) {
            var bytes = new List<byte>();
            using (var gzStr = new GZipStream(new MemoryStream(input), CompressionMode.Decompress)) {
                var bytesRead = new byte[512];
                while (true) {
                    var numRead = gzStr.Read(bytesRead, 0, 512);
                    if (numRead > 0) {
                        bytes.AddRange(bytesRead.Take(numRead));
                    } else {
                        break;
                    }
                }
            }

            return bytes.ToArray().ToUtf8String();
        }
Ejemplo n.º 55
0
        /// <summary>
        ///     Down load a file from the specified URL, uncompress if needed.
        /// </summary>
        /// <param name="url">THe URL.</param>
        /// <param name="file">The file to down load into.</param>
        private void DownloadPage(Uri url, FileInfo file)
        {
            try
            {
                // download the URL
                file.Delete();
                FileInfo tempFile = FileUtil.CombinePath(
                    new FileInfo(file.DirectoryName), "temp.tmp");
                tempFile.Delete();
                FileStream fos        = tempFile.OpenWrite();
                int        lastUpdate = 0;

                int        length   = 0;
                int        size     = 0;
                var        buffer   = new byte[BotUtil.BufferSize];
                WebRequest http     = WebRequest.Create(url);
                var        response = (HttpWebResponse)http.GetResponse();
                Stream     istream  = response.GetResponseStream();


                do
                {
                    length = istream.Read(buffer, 0, buffer.Length);
                    if (length > 0)
                    {
                        if (length >= 0)
                        {
                            fos.Write(buffer, 0, length);
                            size += length;
                        }

                        if (lastUpdate > UpdateTime)
                        {
                            Report(0, (int)(size / Format.MemoryMeg),
                                   "Downloading... " + Format.FormatMemory(size));
                            lastUpdate = 0;
                        }
                        lastUpdate++;
                    }
                } while (length > 0);
                fos.Close();

                // unzip if needed

                if (url.ToString().ToLower().EndsWith(".gz"))
                {
                    // Get the stream of the source file.
                    using (FileStream inFile = tempFile.OpenRead())
                    {
                        //Create the decompressed file.
                        using (FileStream outFile = file.Create())
                        {
                            using (var Decompress = new GZipStream(inFile,
                                                                   CompressionMode.Decompress))
                            {
                                size       = 0;
                                lastUpdate = 0;

                                do
                                {
                                    length = Decompress.Read(buffer, 0, buffer.Length);

                                    if (length >= 0)
                                    {
                                        outFile.Write(buffer, 0, length);
                                        size += length;
                                    }

                                    if (lastUpdate > UpdateTime)
                                    {
                                        Report(0, (int)(size / Format.MemoryMeg),
                                               "Uncompressing... " + Format.FormatMemory(size));
                                        lastUpdate = 0;
                                    }
                                    lastUpdate++;
                                } while (length > 0);
                            }
                        }
                        tempFile.Delete();
                    }
                }
                else
                {
                    // rename the temp file to the actual file
                    file.Delete();
                    tempFile.MoveTo(file.ToString());
                }
            }
            catch (IOException e)
            {
                throw new AnalystError(e);
            }
        }
Ejemplo n.º 56
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);
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 57
0
        public static void WriteTo(string fileName, Repository repository,
                                   Uri artifactUri)
        {
            int n;

            byte[] bytes = new byte[4096];

            // setup request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(artifactUri);

            request.Credentials = repository.Credentials;
            request.Timeout     = 90000;
            //request.KeepAlive = false;
            //request.ProtocolVersion = HttpVersion.Version10;

            // write output to file
            string tempFileName = Path.GetTempFileName();
            string contentType;

            using (WebResponse response = request.GetResponse())
            {
                contentType = response.ContentType;
                using (Stream stream = response.GetResponseStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(File.Create(tempFileName)))
                    {
                        while ((n = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            writer.Write(bytes, 0, n);
                        }
                    }
                }
            }

            // clear out old file as needed
            if (File.Exists(fileName))
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
                File.Delete(fileName);
            }

            // do we need to decompress the data?
            if (contentType != "application/gzip")
            {
                File.Move(tempFileName, fileName);
            }
            else
            {
                // note: mono stackdumped when i tried to do GZipStream over response.GetResponseStream
                // with thousands of files (see: http://lists.ximian.com/pipermail/mono-devel-list/2007-March/022712.html)
                // so this may not be as fast, but it works
                using (GZipStream stream = new GZipStream(File.OpenRead(tempFileName), CompressionMode.Decompress))
                {
                    using (BinaryWriter writer = new BinaryWriter(File.Create(fileName)))
                    {
                        while ((n = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            writer.Write(bytes, 0, n);
                        }
                    }
                }

                File.Delete(tempFileName);
            }
        }
        /// <summary>
        /// Will load a RFE standard file from disk. If the file format is incorrect (unknown) will return false but will not invalidate the internal container
        /// If there are file exceptions, will be received by the caller so should react with appropriate error control
        /// If file is successfully loaded, all previous container data is lost and replaced by data from file
        /// </summary>
        /// <param name="sFile">File name to load</param>
        /// <param name="sModelText">model data text. If it is a normal sweep file, then this comes from spectrum analyzer. If it is tracking or normalization
        /// then this is from both signal generator and spectrum analyzer</param>
        /// <param name="sConfigurationText">configuration text. If it is a normal sweep file, then this comes from spectrum analyzer. If it is tracking or normalization
        /// then this is from Signal Generator and some required parameters from spectrum analyzer too.</param>
        /// <returns></returns>
        public bool LoadFile(string sFile, out string sModelText, out string sConfigurationText)
        {
            sConfigurationText = "Configuration info Unknown - Old file format";
            sModelText         = "Model Unknown - Old file format";
            FileStream myFile = null;

            try
            {
                myFile = new FileStream(sFile, FileMode.Open);

                using (BinaryReader binStream = new BinaryReader(myFile))
                {
                    myFile = null;

                    string sHeader = binStream.ReadString();
                    if ((sHeader != FileHeaderVersioned()) && (sHeader != FileHeaderVersioned_001() && sHeader != FileHeaderVersioned_002() &&
                                                               sHeader != FileHeaderVersioned_003() && sHeader != FileHeaderVersioned_004()))
                    {
                        //unknown format
                        return(false);
                    }

                    double fStartFrequencyMHZ = binStream.ReadDouble();
                    double fStepFrequencyMHZ  = binStream.ReadDouble(); //For normalization file older than v004, we do not use step but start/stop so this variable has not effect
                    UInt32 nMaxDataIndex      = 0;
                    if (sHeader == FileHeaderVersioned_001())
                    {
                        //in version 001 we saved a 16 bits integer
                        nMaxDataIndex = binStream.ReadUInt16();
                    }
                    else
                    {
                        nMaxDataIndex = binStream.ReadUInt32();
                    }

                    UInt16 nTotalDataPoints = binStream.ReadUInt16();

                    if (sHeader != FileHeaderVersioned_001())
                    {
                        sConfigurationText = "From file: " + binStream.ReadString();
                        sModelText         = "From file: " + binStream.ReadString();

                        if (sHeader == FileHeaderVersioned_002() || sHeader == FileHeaderVersioned_003())
                        {
                            //Configuration string previous v004 has [*]RFEGen mark, so it is necessary remove it.
                            int nIndRFEGen = sModelText.IndexOf(_RFEGEN_FILE_MODEL_Mark);
                            sModelText         = sModelText.Substring(nIndRFEGen + 1 + _RFEGEN_FILE_MODEL_Mark.Length);
                            nIndRFEGen         = sConfigurationText.IndexOf(_RFEGEN_FILE_MODEL_Mark);
                            sConfigurationText = sConfigurationText.Substring(nIndRFEGen + 1 + _RFEGEN_FILE_MODEL_Mark.Length);
                        }
                    }

                    if ((sHeader == FileHeaderVersioned_001() || sHeader == FileHeaderVersioned_002() ||
                         sHeader == FileHeaderVersioned_003() || sHeader == FileHeaderVersioned_004()) && (sConfigurationText.Contains(",")))
                    {
                        //From format v005, we always use "en-US" settings. User should create new file.
                        return(false);
                    }

                    //We initialize internal data only if the file was ok and of the right format
                    CleanAll();
                    m_arrData = new RFESweepData[nMaxDataIndex];

                    using (MemoryStream StreamDecompressed = new MemoryStream())
                    {
                        int          nUncompressedSize     = 0;         //total bytes after unzip file, or 0 if was never compressed
                        BinaryReader objBinReader          = binStream; //by default use file stream, but may change to memory if comes compressed
                        byte[]       arrUncompressedBuffer = null;
                        if ((sHeader != FileHeaderVersioned_001()) && (sHeader != FileHeaderVersioned_002()))
                        {
                            nUncompressedSize = (int)binStream.ReadInt32();
                            int nCompressedSize = (int)binStream.ReadInt32();
                            arrUncompressedBuffer = new byte[nUncompressedSize];

                            using (MemoryStream ms = new MemoryStream())
                            {
                                //if we are in version 3 or higher, data comes compressed, so we have to decompress first
                                byte[] gzBuffer = binStream.ReadBytes(nCompressedSize);
                                ms.Write(gzBuffer, 0, nCompressedSize);

                                ms.Position = 0;
                                using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                                {
                                    zip.Read(arrUncompressedBuffer, 0, nUncompressedSize);
                                }
                                StreamDecompressed.Write(arrUncompressedBuffer, 0, nUncompressedSize);
                                StreamDecompressed.Position = 0;
                                objBinReader = new BinaryReader(StreamDecompressed);
                            }
                        }
                        //recreate all sweep data objects
                        for (UInt32 nSweepInd = 0; nSweepInd < nMaxDataIndex; nSweepInd++)
                        {
                            //Versions older than v004 need to add data point extra
                            UInt16 nTotalDataPointsFile = nTotalDataPoints;
                            if (m_eRFEDataType == RFEFileDataType.Normalization)
                            {
                                if (sHeader == FileHeaderVersioned_001() || sHeader == FileHeaderVersioned_002() || sHeader == FileHeaderVersioned_003())
                                {
                                    nTotalDataPoints++;
                                }
                            }
                            RFESweepData objRead = new RFESweepData((float)fStartFrequencyMHZ, (float)fStepFrequencyMHZ, nTotalDataPoints);

                            if (sHeader == FileHeaderVersioned_001())
                            {
                                objRead.CaptureTime = new DateTime(2000, 1, 1); //year 2000 means no actual valid date-time was captured
                            }
                            else
                            {
                                //Starting in version 002, load sweep capture time too
                                int    nLength = (int)objBinReader.ReadInt32();
                                string sTime   = "";
                                if ((sHeader == FileHeaderVersioned_001()) || (sHeader == FileHeaderVersioned_002()))
                                {
                                    sTime = (string)objBinReader.ReadString();
                                }
                                else
                                {
                                    //From v003 we need to decode byte to string for Date/time data
                                    byte[] arrText = objBinReader.ReadBytes(nLength);
                                    sTime = Encoding.ASCII.GetString(arrText);
                                }
                                if ((sTime.Length == nLength) && (nLength > 0))
                                {
                                    objRead.CaptureTime = DateTime.Parse(sTime);
                                }
                            }
                            float fLastPointValue = 0f;
                            for (UInt16 nDataPoint = 0; nDataPoint < nTotalDataPointsFile; nDataPoint++)
                            {
                                fLastPointValue = (float)objBinReader.ReadDouble();
                                objRead.SetAmplitudeDBM(nDataPoint, fLastPointValue);
                            }
                            if (m_eRFEDataType == RFEFileDataType.Normalization)
                            {
                                //Starting in v004. Fix the last point issue, we will add one point extra with the same amplitude value than previous one.
                                if (sHeader == FileHeaderVersioned_001() || sHeader == FileHeaderVersioned_002() || sHeader == FileHeaderVersioned_003())
                                {
                                    objRead.SetAmplitudeDBM((UInt16)(nTotalDataPoints - 1), fLastPointValue);
                                }
                            }
                            Add(objRead);
                        }
                        if (objBinReader != binStream)
                        {
                            objBinReader.Close();
                        }
                    }
                }
            }
            finally
            {
                if (myFile != null)
                {
                    myFile.Dispose();
                }
            }

            return(true);
        }
Ejemplo n.º 59
0
        public static void ExtractTarFromGzipStream(Stream stream, string destinationDirectory)
        {
            using (var gstream = new GZipStream(stream, CompressionMode.Decompress))
            {
                var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
                             RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

                var fileBuffer = new byte[1024 * 4];

                while (true)
                {
                    var header = ReadHeader(gstream);

                    if (header == null)
                    {
                        break;
                    }

                    var output = Path.Combine(destinationDirectory, header.Name);

                    if (header.EntryType == EntryType.SymLink)
                    {
                        if (!isUnix)
                        {
                            throw new Exception("Cannot extract symlink on current platform.");
                        }

                        var parentDirectory = Path.GetDirectoryName(output);
                        if (!Directory.Exists(parentDirectory))
                        {
                            Directory.CreateDirectory(parentDirectory);
                        }

                        Symlink.Create(header.LinkName, output);
                    }

                    if (header.Size > 0)
                    {
                        var parentDirectory = Path.GetDirectoryName(output);
                        if (!Directory.Exists(parentDirectory))
                        {
                            Directory.CreateDirectory(parentDirectory);
                        }

                        using (var fs = File.Open(output, FileMode.Create, FileAccess.Write))
                        {
                            var byteToRead = header.Size;
                            while (byteToRead > 0)
                            {
                                var bytesRead = gstream.Read(
                                    fileBuffer,
                                    0,
                                    (int)Math.Min(byteToRead, (ulong)fileBuffer.Length));
                                if (bytesRead == 0)
                                {
                                    throw new Exception("Couldn't read bytes.");
                                }

                                fs.Write(fileBuffer, 0, bytesRead);

                                byteToRead -= (ulong)bytesRead;
                            }
                        }

                        if (isUnix)
                        {
                            // This OS supports file modes.
                            // Let's set them.
                            Chmod.Set(output, header.Mode);
                        }

                        var trailing = 512 - (int)(header.Size % 512);
                        if (trailing == 512)
                        {
                            trailing = 0;
                        }
                        if (trailing > 0)
                        {
                            if (gstream.Read(fileBuffer, 0, trailing) != trailing)
                            {
                                throw new Exception("Couldn't read bytes");
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 60
-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];
     }
 }