예제 #1
0
 public LzStream(string path, bool useCreateFile = true)
 {
     _handle = useCreateFile
         ? CompressionMethods.LzCreateFile(path, out string uncompressedName)
         : CompressionMethods.LzOpenFile(path, out uncompressedName);
     UncompressedName = uncompressedName;
 }
예제 #2
0
 public void ExpandedName_NotLzFile()
 {
     using (var cleaner = new TestFileCleaner())
     {
         string path = cleaner.CreateTestFile("ExpandedName_NotLzFile");
         CompressionMethods.GetExpandedName(path).Should().Be(path);
     }
 }
예제 #3
0
            public ArchiveCreatorParams(string arc_path,
                                        ArchiveEntry[] arc_entries, ArchiveTypes arc_type, CompressionMethods comp_methd)
            {
                this.archive_path    = arc_path;
                this.archive_entries = arc_entries;

                this.archive_type    = arc_type;
                this.compress_method = comp_methd;
            }
예제 #4
0
        public void CopyFile_OverExisting(bool useCreateFile)
        {
            using (var cleaner = new TestFileCleaner())
            {
                string source      = cleaner.CreateTestFile(CompressedFile2);
                string destination = cleaner.CreateTestFile($"CopyFile_OverExisting({useCreateFile})");

                CompressionMethods.LzCopyFile(source, destination, overwrite: true, useCreateFile: useCreateFile).Should().Be(563);
                FileHelper.ReadAllText(destination).Should().Be(CompressedContent2);
            }
        }
예제 #5
0
 public void OpenFile_LongPath()
 {
     // The OpenFile api only supports 128 character paths.
     using (var cleaner = new TestFileCleaner())
     {
         string path = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 160);
         FileHelper.WriteAllBytes(path, CompressedFile1);
         Action action = () => CompressionMethods.LzOpenFile(path);
         action.ShouldThrow <LzException>().And.Error.Should().Be(LzError.BadInHandle);
     }
 }
예제 #6
0
        public void CopyFile_NotOverExisting(bool useCreateFile)
        {
            using (var cleaner = new TestFileCleaner())
            {
                string source      = cleaner.CreateTestFile(CompressedFile2);
                string destination = cleaner.CreateTestFile($"CopyFile_NotOverExisting({useCreateFile})");

                Action action = () => CompressionMethods.LzCopyFile(source, destination, overwrite: false, useCreateFile: useCreateFile);
                action.ShouldThrow <IOException>().And.HResult.Should().Be((int)ErrorMacros.HRESULT_FROM_WIN32(WindowsError.ERROR_FILE_EXISTS));
            }
        }
예제 #7
0
 private void _read()
 {
     _version            = new Version(m_io, this, m_root);
     _random             = new Random(m_io, this, m_root);
     _sessionId          = new SessionId(m_io, this, m_root);
     _cipherSuites       = new CipherSuites(m_io, this, m_root);
     _compressionMethods = new CompressionMethods(m_io, this, m_root);
     if (M_Io.IsEof == false)
     {
         _extensions = new Extensions(m_io, this, m_root);
     }
 }
예제 #8
0
        public void CopyFile(bool useCreateFile)
        {
            using (var cleaner = new TestFileCleaner())
            {
                string source      = cleaner.CreateTestFile(CompressedFile2);
                string destination = cleaner.GetTestPath();

                CompressionMethods.LzCopyFile(source, destination, false, useCreateFile).Should().Be(563);

                FileHelper.ReadAllText(destination).Should().Be(CompressedContent2);
            }
        }
예제 #9
0
 public void ExpandedNameEx(string compressedName, byte character, string expandedName)
 {
     using (var cleaner = new TestFileCleaner())
     {
         byte[] data = new byte[CompressedFile1.Length];
         CompressedFile1.CopyTo(data, 0);
         data[9] = character;
         string path = Paths.Combine(cleaner.TempFolder, compressedName);
         FileHelper.WriteAllBytes(path, data);
         Path.GetFileName(CompressionMethods.GetExpandedNameEx(path)).Should().Be(expandedName);
     }
 }
예제 #10
0
 public void CreateFile_OverMaxPathLongPath()
 {
     // Unlike OpenFile, CreateFile handles > 128 character paths. Unfortunately it is
     // constrained by MAX_PATH internal buffers, so it cant go over 260.
     using (var cleaner = new TestFileCleaner())
     {
         string path = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 300);
         FileHelper.EnsurePathDirectoryExists(path);
         FileHelper.WriteAllBytes(path, CompressedFile1);
         Action action = () => CompressionMethods.LzCreateFile(path);
         action.ShouldThrow <LzException>().And.Error.Should().Be(LzError.BadValue);
     }
 }
예제 #11
0
 public void CreateFile_LongPath()
 {
     // Unlike OpenFile, CreateFile handles > 128 character paths.
     using (var cleaner = new TestFileCleaner())
     {
         string path = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 160);
         FileHelper.WriteAllBytes(path, CompressedFile1);
         using (var handle = CompressionMethods.LzCreateFile(path))
         {
             handle.RawHandle.Should().BeGreaterThan(0);
         }
     }
 }
예제 #12
0
        public void ExpandedName_LongPath()
        {
            // Unfortunately GetExpandedNameW doesn't fail properly. It calls the A version
            // and accidentally ignores the errors returned, copying garbage into the
            // returned string.

            using (var cleaner = new TestFileCleaner())
            {
                string path = PathGenerator.CreatePathOfLength(cleaner.TempFolder, 160);
                FileHelper.WriteAllBytes(path, CompressedFile1);
                Action action = () => CompressionMethods.GetExpandedName(path);
                action.ShouldThrow <LzException>().And.Error.Should().Be(LzError.BadValue);
            }
        }
예제 #13
0
        public static string Decompress(this string text, CompressionMethods method = CompressionMethods.gzip)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(string.Empty);
            }

            if (method == CompressionMethods.gzip)
            {
                return(Unzip(text));
            }
            else
            {
                throw new NotSupportedException();
            }
        }
예제 #14
0
        public string Unzip(string value, CompressionMethods method = CompressionMethods.gzip)
        {
            var gZipBuffer = Convert.FromBase64String(value);

            using (var memoryStream = new MemoryStream())
            {
                var dataLength = BitConverter.ToInt32(gZipBuffer, 0);
                memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
                var buffer = new byte[dataLength];
                memoryStream.Position = 0;
                using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    gZipStream.Read(buffer, 0, buffer.Length);
                }
                return(Encoding.UTF8.GetString(buffer));
            }
        }
예제 #15
0
        public string Zip(string value, CompressionMethods method = CompressionMethods.gzip)
        {
            var buffer = Encoding.UTF8.GetBytes(value);

            using (var memoryStream = new MemoryStream())
            {
                using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
                {
                    gZipStream.Write(buffer, 0, buffer.Length);
                }
                memoryStream.Position = 0;
                var compressedData = new byte[memoryStream.Length];
                memoryStream.Read(compressedData, 0, compressedData.Length);
                var gZipBuffer = new byte[compressedData.Length + 4];
                Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
                Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
                return(Convert.ToBase64String(gZipBuffer));
            }
        }