Example #1
0
        /// <summary>
        /// Unwraps the provided file with either a callback or the target directory
        /// </summary>
        /// <param name="wrapperFile">the file that contains the wrapped content</param>
        /// <param name="targetFile">the target file into which the content is unwrapped</param>
        /// <param name="unwrappedFiles">a fileMap that contains the unwrapped item</param>
        /// <param name="performUnzip">indicates whether to actually perform the unpacking process</param>
        /// <param name="requestStream">a callback that is used to create a new stream if required</param>
        /// <returns>a value indicating whether the unwrapping was successful</returns>
        private bool UnwrapFile(Stream wrapperFile, string targetFile, FileMap unwrappedFiles, bool performUnzip,
                                Func <string, Stream> requestStream = null)
        {
            FileMapEntry ent;

            unwrappedFiles.Add(ent = new FileMapEntry(Path.GetFileName(targetFile)));
            if (performUnzip)
            {
                using (GZipInputStream gzis = new GZipInputStream(wrapperFile))
                {
                    using (

                        /*Stream fso = (requestStream == null)
                         *           ? new FileStream(targetFile, FileMode.Create, FileAccess.ReadWrite)
                         *           : (requestStream(targetFile) ??
                         *              new FileStream(targetFile, FileMode.Create, FileAccess.ReadWrite)))*/
                        var fso = ent.Open())
                    {
                        gzis.CopyTo(fso);
                    }
                }
            }

            return(true);
        }
Example #2
0
        /// <summary>
        /// Unwraps a zip into a given directory
        /// </summary>
        /// <param name="zipFile">the zip-file to unwrap into the targetdirectory</param>
        /// <param name="targetDirectory">the targetdirectory in which to extract all files</param>
        /// <param name="performUnzip">indicates whether to perform the actual unpacking process</param>
        /// <param name="getStreamCallback">callback that is used to provide a stream for unwrapped content</param>
        /// <param name="copyAction">An action defining what happens while copying the content of the Stream to the target file</param>
        /// <param name="ignoreNullStreams">indicates whether to prevent files, for which the getStreamCallback method returns null, from being unwrapped</param>
        /// <param name="password">the password that is used to access the zip file</param>
        /// <returns>an array containing all extracted filenames</returns>
        private FileMap UnwrapZip(Stream zipFile, string targetDirectory, bool performUnzip, Func <FileMapEntry, Stream> getStreamCallback = null, Action <Stream, Stream> copyAction = null, bool ignoreNullStreams = false, string password = null)
        {
            FileMap map = new FileMap(targetDirectory, flattenStructure);

            if (copyAction == null)
            {
                copyAction = (input, output) =>
                {
                    try
                    {
                        input.CopyTo(output);
                    }
                    finally
                    {
                        output.Dispose();
                    }
                };
            }
            using (ZipFile fl = new ZipFile(zipFile))
            {
                if (password != null)
                {
                    fl.Password = password;
                }

                foreach (ZipEntry ent in fl)
                {
                    if (!ent.IsDirectory)
                    {
                        FileMapEntry entry = new FileMapEntry(ent.Name);
                        map.Add(entry);
                        if (performUnzip)
                        {
                            using (Stream s = fl.GetInputStream(ent))
                            {
                                Stream fs = (getStreamCallback == null)
                                    ? entry.Open()
                                    : getStreamCallback(entry);

                                if (fs == null && !ignoreNullStreams)
                                {
                                    fs = entry.Open();
                                }

                                if (fs != null)
                                {
                                    copyAction(s, fs);
                                }
                            }
                        }
                    }
                }
            }

            return(map);
        }
Example #3
0
        /// <summary>
        /// Untars a file by either using the target directory or streams provided by the getStreamCallback function
        /// </summary>
        /// <param name="targetDirectory">the target directory into which to export the unwrapped files</param>
        /// <param name="tarStream">the tarstream that is used to export the files</param>
        /// <param name="unpack">indicates whether to actually perform the unpacking process</param>
        /// <param name="getStreamCallback">a callback that provides streams for each unwrapped file</param>
        /// <param name="copyAction">the action to execute for copying the content of the stream to the target</param>
        /// <param name="ignoreNullStreams">indicates whether to prevent using the default-directory when getStreamCallback does return null</param>
        /// <returns>a filemap that contains all unwrapped files and their result/source paths</returns>
        private FileMap UnTarFiles(string targetDirectory, TarStreamHelper tarStream, bool unpack,
                                   [InstantHandle] Func <FileMapEntry, Stream> getStreamCallback = null, [InstantHandle] Action <Stream, Stream> copyAction = null, bool ignoreNullStreams = false)
        {
            FileMap  map = new FileMap(targetDirectory, flattenStructure);
            TarEntry entry;

            if (copyAction == null)
            {
                copyAction = (input, output) =>
                {
                    try
                    {
                        input.CopyTo(output);
                    }
                    finally
                    {
                        output.Dispose();
                    }
                };
            }

            while ((entry = tarStream.InputStream.GetNextEntry()) != null)
            {
                if (!entry.IsDirectory)
                {
                    FileMapEntry ent = new FileMapEntry(entry.Name);
                    map.Add(ent);
                    if (unpack)
                    {
                        Stream fout = (getStreamCallback == null)
                            ? ent.Open()
                            : getStreamCallback(ent);

                        if (fout == null && !ignoreNullStreams)
                        {
                            fout = ent.Open();
                        }

                        if (fout != null)
                        {
                            copyAction(tarStream.InputStream, fout);
                        }
                    }
                }
            }

            return(map);
        }
Example #4
0
 /// <summary>
 /// Writes an entry from an original file into the given zip file
 /// </summary>
 /// <param name="originalFile">the original file that is located in the filesystem</param>
 /// <param name="entry">an instance representing the zip-entry that is to be written</param>
 /// <param name="outputStream">the output stream in which to put the provided item</param>
 /// <param name="keySize">the keySize of the Zip Entry that is written</param>
 /// <returns>a value indicating whether the creation of the entry was successful(always true...)</returns>
 private bool WriteEntry(FileMapEntry originalFile, ZipEntry entry, ZipOutputStream outputStream, int keySize)
 {
     try
     {
         if (keySize > 0)
         {
             entry.AESKeySize = keySize;
         }
         outputStream.PutNextEntry(entry);
         using (Stream inputStream = originalFile.Open())
         {
             inputStream.CopyTo(outputStream);
         }
     }
     finally
     {
         outputStream.CloseEntry();
     }
     return(true);
 }