Ejemplo n.º 1
0
        public SharpZipTarArchiveIterator(Stream compressedTarFile, CompressionFactory.Type compressionType)
        {
            if (compressionType == CompressionFactory.Type.Gz)
            {
                compressionStream = CompressionFactory.Reader(CompressionFactory.Type.Gz, compressedTarFile);
            }
            else if (compressionType == CompressionFactory.Type.Bz2)
            {
                compressionStream = CompressionFactory.Reader(CompressionFactory.Type.Bz2, compressedTarFile);
            }
            else
            {
                throw new NotSupportedException($"Type {compressionType} is not supported by ArchiveIterator");
            }

            tarStream = new TarInputStream(compressionStream);
            disposed  = false;
        }
Ejemplo n.º 2
0
        private static void ProcessCompression(EnvelopeType env, string ovfPath, bool compress,
                                               Action cancellingDelegate, CompressionFactory.Type method = CompressionFactory.Type.Gz)
        {
            if (env.References?.File == null)
            {
                return;
            }

            string path = Path.GetDirectoryName(ovfPath);

            foreach (File_Type file in env.References.File)
            {
                if (!compress)
                {
                    if (file.compression == null)
                    {
                        log.InfoFormat("File {0} was not marked as compressed, skipped.", file.href);
                        continue;
                    }

                    if (file.compression.ToLower() == "gzip")
                    {
                        method = CompressionFactory.Type.Gz;
                    }
                    else if (file.compression.ToLower() == "bzip2")
                    {
                        method = CompressionFactory.Type.Bz2;
                    }
                    else
                    {
                        log.ErrorFormat("File {0} uses unsupported method {1}. Must be Gzip or BZip2. Skipping.",
                                        file.href, file.compression);
                        continue;
                    }
                }

                int    slash    = file.href.LastIndexOf('/');
                string stem     = slash >= 0 ? file.href.Substring(0, slash + 1) : "";
                string filePath = Path.Combine(path, slash >= 0 ? file.href.Substring(slash + 1) : file.href);
                string tempfile = Path.Combine(path, Path.GetRandomFileName());

                try
                {
                    if (compress)
                    {
                        CompressionFactory.CompressFile(filePath, tempfile, method, cancellingDelegate);
                    }
                    else
                    {
                        CompressionFactory.UncompressFile(filePath, tempfile, method, cancellingDelegate);
                    }

                    File.Delete(filePath);
                    var ext = method.FileExtension();

                    if (compress)
                    {
                        filePath += ext;
                    }
                    else if (filePath.EndsWith(ext))
                    {
                        filePath = filePath.Substring(0, filePath.Length - ext.Length);
                    }

                    File.Move(tempfile, filePath);
                    file.href        = stem + Path.GetFileName(filePath);
                    file.compression = compress ? method.StringOf() : null;
                    FileInfo fi = new FileInfo(filePath);
                    file.size = (ulong)fi.Length;
                }
                catch (EndOfStreamException eose)
                {
                    log.Error("End of Stream: ", eose);
                }
                finally
                {
                    try
                    {
                        File.Delete(tempfile);
                    }
                    catch
                    {
                        //ignore
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public static void CompressFiles(EnvelopeType env, string ovfPath, CompressionFactory.Type method, Action cancellingDelegate)
 {
     ProcessCompression(env, ovfPath, true, cancellingDelegate, method);
     SaveAs(env, ovfPath);
 }