Пример #1
0
        public Stream OpenRead(string fileName)
        {
            Stream            stream            = File.OpenRead(FullName);
            CompressionEngine compressionEngine = CreateCompressionEngine();

            return(new CargoStream(compressionEngine.Unpack(stream, fileName), stream, compressionEngine));
        }
Пример #2
0
        public void Unpack(string destDirectory, EventHandler <ArchiveProgressEventArgs> progressHandler)
        {
            using CompressionEngine compressionEngine = CreateCompressionEngine();
            compressionEngine.Progress += progressHandler;
            ArchiveFileStreamContext archiveFileStreamContext = new ArchiveFileStreamContext(FullName, destDirectory, null);

            archiveFileStreamContext.EnableOffsetOpen = true;
            compressionEngine.Unpack(archiveFileStreamContext, null);
        }
Пример #3
0
        /// <summary>
        /// Opens a file inside the archive for reading without actually
        /// extracting the file to disk.
        /// </summary>
        /// <param name="fileName">The name of the file in the archive. Also
        /// includes the internal path of the file, if any. File name matching
        /// is case-insensitive.</param>
        /// <returns>
        /// A stream for reading directly from the packed file. Like any stream
        /// this should be closed/disposed as soon as it is no longer needed.
        /// </returns>
        public Stream OpenRead(string fileName)
        {
            Stream            archiveStream     = File.OpenRead(this.FullName);
            CompressionEngine compressionEngine = this.CreateCompressionEngine();
            Stream            fileStream        = compressionEngine.Unpack(archiveStream, fileName);

            // Attach the archiveStream and compressionEngine to the
            // fileStream so they get disposed when the fileStream is disposed.
            return(new CargoStream(fileStream, archiveStream, compressionEngine));
        }
Пример #4
0
        public void UnpackFileSet(IDictionary <string, string> fileNames, string destDirectory, EventHandler <ArchiveProgressEventArgs> progressHandler)
        {
            if (fileNames == null)
            {
                throw new ArgumentNullException("fileNames");
            }
            using CompressionEngine compressionEngine = CreateCompressionEngine();
            compressionEngine.Progress += progressHandler;
            ArchiveFileStreamContext archiveFileStreamContext = new ArchiveFileStreamContext(FullName, destDirectory, fileNames);

            archiveFileStreamContext.EnableOffsetOpen = true;
            compressionEngine.Unpack(archiveFileStreamContext, (string match) => fileNames.ContainsKey(match));
        }
Пример #5
0
        public static void TestCompressionEngineNullParams(
            CompressionEngine engine,
            ArchiveFileStreamContext streamContext,
            string[] testFiles)
        {
            Exception caughtEx;

            Console.WriteLine("Testing null streamContext.");
            caughtEx = null;
            try
            {
                engine.Pack(null, testFiles);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.Pack(null, testFiles, 0);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);

            Console.WriteLine("Testing null files.");
            caughtEx = null;
            try
            {
                engine.Pack(streamContext, null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);

            Console.WriteLine("Testing null files.");
            caughtEx = null;
            try
            {
                engine.Pack(streamContext, null, 0);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);

            Console.WriteLine("Testing null stream.");
            caughtEx = null;
            try
            {
                engine.IsArchive(null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.FindArchiveOffset(null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.GetFiles(null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.GetFileInfo(null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.Unpack(null, "testUnpack.txt");
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            Console.WriteLine("Testing null streamContext.");
            caughtEx = null;
            try
            {
                engine.GetFiles(null, null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.GetFileInfo(null, null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
            caughtEx = null;
            try
            {
                engine.Unpack((IUnpackStreamContext) null, null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx);
        }
Пример #6
0
        public static void TestBadUnpackStreamContexts(
            CompressionEngine engine, string archiveName)
        {
            Exception caughtEx;

            Console.WriteLine("Testing streamContext that returns null from OpenArchive.");
            caughtEx = null;
            try
            {
                engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, false, true, false, true, true, true), null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsInstanceOfType(caughtEx, typeof(FileNotFoundException), "Caught exception: " + caughtEx);
            Console.WriteLine("Testing streamContext that returns null from OpenFile.");
            caughtEx = null;
            try
            {
                engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, false, true, true, true, false, true), null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsNull(caughtEx, "Caught exception: " + caughtEx);
            Console.WriteLine("Testing streamContext that throws on OpenArchive.");
            caughtEx = null;
            try
            {
                engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, false, true, true, true), null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx);
            Console.WriteLine("Testing streamContext that throws on CloseArchive.");
            caughtEx = null;
            try
            {
                engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, true, false, true, true), null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx);
            Console.WriteLine("Testing streamContext that throws on OpenFile.");
            caughtEx = null;
            try
            {
                engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, true, true, false, true), null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx);
            Console.WriteLine("Testing streamContext that throws on CloseFile.");
            caughtEx = null;
            try
            {
                engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, true, true, true, false), null);
            }
            catch (Exception ex) { caughtEx = ex; }
            Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx);
        }