Esempio n. 1
0
        public static void Extract(string filename, string outputFilename)
        {
            using (GZipStream input = new GZipStream(File.OpenRead(filename), CompressionMode.Decompress))
            {
                MemoryStream m = new MemoryStream();
                copyTo(input, m);
                m.Seek(0, SeekOrigin.Begin);

                tar_cs.TarReader reader = new tar_cs.TarReader(m);

                while (reader.MoveNext(false))
                {
                    if (reader.FileInfo.FileName.EndsWith("/"))
                    {
                        Directory.CreateDirectory(reader.FileInfo.FileName);
                    }
                    else
                    {
                        Stream output = File.Create(reader.FileInfo.FileName);
                        reader.Read(output);
                        output.Close();
                    }
                }

                m.Dispose();
            }
        }
Esempio n. 2
0
        protected Dictionary <string, byte[]> LoadPackage(string path)
        {
            if (path.EndsWith(".tar.lzma"))
            {
                using (var lzma = new FileStream(Path.Combine(m_TestLocation, path), FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (var decompress = new MemoryStream())
                    {
                        LZMA.LzmaHelper.Decompress(lzma, decompress);
                        decompress.Seek(0, SeekOrigin.Begin);

                        var archive      = new tar_cs.TarReader(decompress);
                        var deduplicator = new Reduplicator();
                        return(deduplicator.UnpackTarToMemory(archive));
                    }
                }
            }
            else if (path.EndsWith(".tar.gz"))
            {
                using (var file = new FileStream(Path.Combine(m_TestLocation, path), FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (var gzip = new GZipStream(file, CompressionMode.Decompress))
                    {
                        var archive      = new tar_cs.TarReader(gzip);
                        var deduplicator = new Reduplicator();
                        return(deduplicator.UnpackTarToMemory(archive));
                    }
                }
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Esempio n. 3
0
        private void ExtractTo(string format, byte[] data, string path)
        {
            Console.WriteLine("Unpacking binary package from " + format + " archive");
            switch (format)
            {
            case PackageManager.ARCHIVE_FORMAT_TAR_GZIP:
            {
                using (var memory = new MemoryStream(data))
                {
                    using (var decompress = new GZipStream(memory, CompressionMode.Decompress))
                    {
                        using (var memory2 = new MemoryStream())
                        {
                            decompress.CopyTo(memory2);
                            memory2.Seek(0, SeekOrigin.Begin);
                            var reader       = new tar_cs.TarReader(memory2);
                            var reduplicator = new Reduplicator();
                            reduplicator.UnpackTarToFolder(reader, path);
                        }
                    }
                }
                break;
            }

            case PackageManager.ARCHIVE_FORMAT_TAR_LZMA:
            {
                using (var inMemory = new MemoryStream(data))
                {
                    using (var outMemory = new MemoryStream())
                    {
                        LZMA.LzmaHelper.Decompress(inMemory, outMemory);
                        outMemory.Seek(0, SeekOrigin.Begin);
                        var reader       = new tar_cs.TarReader(outMemory);
                        var reduplicator = new Reduplicator();
                        reduplicator.UnpackTarToFolder(reader, path);
                    }
                }
                break;
            }

            default:
                throw new InvalidOperationException(
                          "This version of Protobuild does not support the " +
                          format + " package format.");
            }
        }
Esempio n. 4
0
        bool Extract(string directory, string localPath)
        {
            EB.Debug.Log("extracting " + localPath + " to " + directory);
            try
            {
                var files = new ArrayList();

                var filestream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
                {
                    var gzipstream = new Pathfinding.Ionic.Zlib.GZipStream(filestream, Pathfinding.Ionic.Zlib.CompressionMode.Decompress);
                    {
                        var tarstream = new tar_cs.TarReader(gzipstream);
                        while (tarstream.MoveNext(false))
                        {
                            var fileInfo = tarstream.FileInfo;

                            string totalPath = Path.Combine(directory, fileInfo.FileName);
                            string fileName  = Path.GetFileName(totalPath);
                            string dir       = totalPath.Remove(totalPath.Length - fileName.Length);
                            Directory.CreateDirectory(dir);

                            EB.Debug.Log("extracting " + fileName);
                            using (FileStream file = new FileStream(totalPath, FileMode.Create, FileAccess.Write))
                            {
                                tarstream.Read(file);
                            }

                            files.Add(new object[] { fileInfo.FileName, fileInfo.SizeInBytes });
                        }
                    }
                }

                // write the verification file
                if (!WriteJsonFile(Path.Combine(directory, "files.txt"), files))
                {
                    return(false);
                }

                return(true);
            }
            catch (System.Exception ex)
            {
                EB.Debug.LogError("extraction failed " + ex);
                return(false);
            }
        }
Esempio n. 5
0
 public void ExtractTo(string path)
 {
     Console.WriteLine("Unpacking binary package from " + this.Format + " archive");
     switch (this.Format)
     {
         case PackageManager.ARCHIVE_FORMAT_TAR_GZIP:
             {
                 using (var memory = new MemoryStream(this.PackageData))
                 {
                     using (var decompress = new GZipStream(memory, CompressionMode.Decompress))
                     {
                         using (var memory2 = new MemoryStream())
                         {
                             decompress.CopyTo(memory2);
                             memory2.Seek(0, SeekOrigin.Begin);
                             var reader = new tar_cs.TarReader(memory2);
                             var reduplicator = new Reduplicator();
                             reduplicator.UnpackTarToFolder(reader, path);
                         }
                     }
                 }
                 break;
             }
         case PackageManager.ARCHIVE_FORMAT_TAR_LZMA:
             {
                 using (var inMemory = new MemoryStream(this.PackageData))
                 {
                     using (var outMemory = new MemoryStream())
                     {
                         LZMA.LzmaHelper.Decompress(inMemory, outMemory);
                         outMemory.Seek(0, SeekOrigin.Begin);
                         var reader = new tar_cs.TarReader(outMemory);
                         var reduplicator = new Reduplicator();
                         reduplicator.UnpackTarToFolder(reader, path);
                     }
                 }
                 break;
             }
         default:
             throw new InvalidOperationException(
                 "This version of Protobuild does not support the " +
                 this.Format + " package format.");
     }
 }
Esempio n. 6
0
        protected Dictionary<string, byte[]> LoadPackage(string path)
        {
            var results = new Dictionary<string, byte[]>();

            if (path.EndsWith(".tar.lzma"))
            {
                using (var lzma = new FileStream(Path.Combine(m_TestLocation, path), FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (var decompress = new MemoryStream())
                    {
                        LZMA.LzmaHelper.Decompress(lzma, decompress);
                        decompress.Seek(0, SeekOrigin.Begin);

                        var archive = new tar_cs.TarReader(decompress);
                        var deduplicator = new Reduplicator();
                        return deduplicator.UnpackTarToMemory(archive);
                    }
                }
            }
            else if (path.EndsWith(".tar.gz"))
            {
                using (var file = new FileStream(Path.Combine(m_TestLocation, path), FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (var gzip = new GZipStream(file, CompressionMode.Decompress))
                    {
                        var archive = new tar_cs.TarReader(gzip);
                        var deduplicator = new Reduplicator();
                        return deduplicator.UnpackTarToMemory(archive);
                    }
                }
            }
            else
            {
                throw new NotSupportedException();
            }

            return results;
        }
Esempio n. 7
0
        private void ExtractTo(string workingDirectory, string packageName, string format, byte[] data, string path, string platform)
        {
            RedirectableConsole.WriteLine("Unpacking binary package from " + format + " archive");
            switch (format)
            {
            case PackageManager.ARCHIVE_FORMAT_TAR_GZIP:
            {
                using (var memory = new MemoryStream(data))
                {
                    using (var decompress = new GZipStream(memory, CompressionMode.Decompress))
                    {
                        using (var memory2 = new MemoryStream())
                        {
                            decompress.CopyTo(memory2);
                            memory2.Seek(0, SeekOrigin.Begin);
                            var reader       = new tar_cs.TarReader(memory2);
                            var reduplicator = new Reduplicator();
                            reduplicator.UnpackTarToFolder(reader, path);
                        }
                    }
                }
                break;
            }

            case PackageManager.ARCHIVE_FORMAT_TAR_LZMA:
            {
                using (var inMemory = new MemoryStream(data))
                {
                    using (var outMemory = new MemoryStream())
                    {
                        LZMA.LzmaHelper.Decompress(inMemory, outMemory);
                        outMemory.Seek(0, SeekOrigin.Begin);
                        var reader       = new tar_cs.TarReader(outMemory);
                        var reduplicator = new Reduplicator();
                        reduplicator.UnpackTarToFolder(reader, path);
                    }
                }
                break;
            }

            case PackageManager.ARCHIVE_FORMAT_NUGET_ZIP:
            {
                using (var inMemory = new MemoryStream(data))
                {
                    using (var zipStorer = ZipStorer.Open(inMemory, FileAccess.Read, true))
                    {
                        var reduplicator   = new Reduplicator();
                        var extractedFiles = reduplicator.UnpackZipToFolder(
                            zipStorer,
                            path,
                            candidatePath => candidatePath.Replace('\\', '/').StartsWith("protobuild/" + platform + "/"),
                            outputPath => outputPath.Replace('\\', '/').Substring(("protobuild/" + platform + "/").Length));

                        if (extractedFiles.Count == 0)
                        {
                            // There were no files that matched protobuild/ which means this is
                            // not a Protobuild-aware NuGet package.  We need to convert it on-the-fly
                            // to a compatible Protobuild format.
                            ConvertNuGetOnlyPackage(reduplicator, zipStorer, path, packageName, workingDirectory, platform);
                        }
                    }
                }
                break;
            }

            default:
                throw new InvalidOperationException(
                          "This version of Protobuild does not support the " +
                          format + " package format.");
            }
        }
Esempio n. 8
0
 public Dictionary <string, byte[]> UnpackTarToMemory(tar_cs.TarReader reader)
 {
     return(UnpackTar(reader, null, true));
 }
Esempio n. 9
0
 public void UnpackTarToFolder(tar_cs.TarReader reader, string folder)
 {
     UnpackTar(reader, folder, false);
 }
Esempio n. 10
0
        private Dictionary <string, byte[]> UnpackTar(tar_cs.TarReader reader, string folder, bool toMemory)
        {
            var results = new Dictionary <string, byte[]>();

            const string DedupPrefix     = "_DedupFiles/";
            var          hashesToStreams = new Dictionary <string, Stream>();

            while (reader.MoveNext(false))
            {
                if (reader.FileInfo.FileName.StartsWith(DedupPrefix) &&
                    reader.FileInfo.EntryType == tar_cs.EntryType.File)
                {
                    // This is a deduplicated archive; place the deduplicated
                    // files into the dictionary.
                    var hash   = reader.FileInfo.FileName.Substring(DedupPrefix.Length);
                    var memory = new MemoryStream();
                    reader.Read(memory);
                    memory.Seek(0, SeekOrigin.Begin);
                    hashesToStreams.Add(hash, memory);
                    continue;
                }

                if (reader.FileInfo.FileName == DedupPrefix &&
                    reader.FileInfo.EntryType == tar_cs.EntryType.Directory)
                {
                    // This is the deduplication folder; ignore it.
                    continue;
                }

                var target = folder == null ? null : Path.Combine(folder, reader.FileInfo.FileName);

                try
                {
                    switch (reader.FileInfo.EntryType)
                    {
                    case tar_cs.EntryType.File:
                    case tar_cs.EntryType.FileObsolete:
                        if (toMemory)
                        {
                            using (var memory = new MemoryStream())
                            {
                                reader.Read(memory);
                                var data = new byte[(int)memory.Position];
                                memory.Seek(0, SeekOrigin.Begin);
                                memory.Read(data, 0, data.Length);
                                results.Add(reader.FileInfo.FileName, data);
                            }
                        }
                        else
                        {
                            using (var writer = new FileStream(target, FileMode.Create, FileAccess.Write, FileShare.None))
                            {
                                reader.Read(writer);
                            }
                        }
                        break;

                    case tar_cs.EntryType.HardLink:
                        if (reader.FileInfo.LinkName.StartsWith(DedupPrefix))
                        {
                            // This file has been deduplicated; resolve the actual file
                            // using the dictionary.
                            var hash = reader.FileInfo.LinkName.Substring(DedupPrefix.Length);
                            if (!hashesToStreams.ContainsKey(hash))
                            {
                                Console.WriteLine("WARNING: Unable to find deduplicated stream for file hash " +
                                                  hash);
                            }
                            else
                            {
                                if (toMemory)
                                {
                                    using (var memory = new MemoryStream())
                                    {
                                        hashesToStreams[hash].CopyTo(memory);
                                        hashesToStreams[hash].Seek(0, SeekOrigin.Begin);
                                        var data = new byte[(int)memory.Position];
                                        memory.Seek(0, SeekOrigin.Begin);
                                        memory.Read(data, 0, data.Length);
                                        results.Add(reader.FileInfo.FileName, data);
                                    }
                                }
                                else
                                {
                                    using (var writer = new FileStream(target, FileMode.Create, FileAccess.Write,
                                                                       FileShare.None))
                                    {
                                        hashesToStreams[hash].CopyTo(writer);
                                        hashesToStreams[hash].Seek(0, SeekOrigin.Begin);
                                    }
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine("WARNING: Unknown hard link present in TAR archive.");
                        }
                        break;

                    case tar_cs.EntryType.Directory:
                        if (toMemory)
                        {
                            results.Add(reader.FileInfo.FileName.Trim('/') + "/", null);
                        }
                        else
                        {
                            Directory.CreateDirectory(target);
                        }
                        break;

                    default:
                        Console.WriteLine("WARNING: Ignoring unknown entry type in TAR archive.");
                        break;
                    }
                }
                catch (PathTooLongException ex)
                {
                    throw new PathTooLongException("The path '" + target + "' is too long to extract on this operating system.", ex);
                }
            }

            return(results);
        }
Esempio n. 11
0
        public Dictionary <string, byte[]> UnpackTarToMemory(tar_cs.TarReader reader)
        {
            const string DedupPrefix     = "_DedupFiles/";
            var          hashesToStreams = new Dictionary <string, Stream>();
            var          results         = new Dictionary <string, byte[]>();

            while (reader.MoveNext(false))
            {
                if (reader.FileInfo.FileName.StartsWith(DedupPrefix) &&
                    reader.FileInfo.EntryType == tar_cs.EntryType.File)
                {
                    // This is a deduplicated archive; place the deduplicated
                    // files into the dictionary.
                    var hash   = reader.FileInfo.FileName.Substring(DedupPrefix.Length);
                    var memory = new MemoryStream();
                    reader.Read(memory);
                    memory.Seek(0, SeekOrigin.Begin);
                    hashesToStreams.Add(hash, memory);
                    continue;
                }

                if (reader.FileInfo.FileName == DedupPrefix &&
                    reader.FileInfo.EntryType == tar_cs.EntryType.Directory)
                {
                    // This is the deduplication folder; ignore it.
                    continue;
                }

                switch (reader.FileInfo.EntryType)
                {
                case tar_cs.EntryType.File:
                case tar_cs.EntryType.FileObsolete:
                    using (var memory = new MemoryStream())
                    {
                        reader.Read(memory);
                        var data = new byte[(int)memory.Position];
                        memory.Seek(0, SeekOrigin.Begin);
                        memory.Read(data, 0, data.Length);
                        results.Add(reader.FileInfo.FileName, data);
                    }
                    break;

                case tar_cs.EntryType.HardLink:
                    if (reader.FileInfo.LinkName.StartsWith(DedupPrefix))
                    {
                        // This file has been deduplicated; resolve the actual file
                        // using the dictionary.
                        var hash = reader.FileInfo.LinkName.Substring(DedupPrefix.Length);
                        if (!hashesToStreams.ContainsKey(hash))
                        {
                            Console.WriteLine("WARNING: Unable to find deduplicated stream for file hash " + hash);
                        }
                        else
                        {
                            using (var memory = new MemoryStream())
                            {
                                hashesToStreams[hash].CopyTo(memory);
                                hashesToStreams[hash].Seek(0, SeekOrigin.Begin);
                                var data = new byte[(int)memory.Position];
                                memory.Seek(0, SeekOrigin.Begin);
                                memory.Read(data, 0, data.Length);
                                results.Add(reader.FileInfo.FileName, data);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("WARNING: Unknown hard link present in TAR archive.");
                    }
                    break;

                case tar_cs.EntryType.Directory:
                    results.Add(reader.FileInfo.FileName.Trim('/') + "/", null);
                    break;

                default:
                    Console.WriteLine("WARNING: Ignoring unknown entry type in TAR archive.");
                    break;
                }
            }

            return(results);
        }
Esempio n. 12
0
        public void UnpackTarToFolder(tar_cs.TarReader reader, string folder)
        {
            const string DedupPrefix     = "_DedupFiles/";
            var          hashesToStreams = new Dictionary <string, Stream>();

            while (reader.MoveNext(false))
            {
                if (reader.FileInfo.FileName.StartsWith(DedupPrefix) &&
                    reader.FileInfo.EntryType == tar_cs.EntryType.File)
                {
                    // This is a deduplicated archive; place the deduplicated
                    // files into the dictionary.
                    var hash   = reader.FileInfo.FileName.Substring(DedupPrefix.Length);
                    var memory = new MemoryStream();
                    reader.Read(memory);
                    memory.Seek(0, SeekOrigin.Begin);
                    hashesToStreams.Add(hash, memory);
                    continue;
                }

                if (reader.FileInfo.FileName == DedupPrefix &&
                    reader.FileInfo.EntryType == tar_cs.EntryType.Directory)
                {
                    // This is the deduplication folder; ignore it.
                    continue;
                }

                switch (reader.FileInfo.EntryType)
                {
                case tar_cs.EntryType.File:
                case tar_cs.EntryType.FileObsolete:
                    using (var writer = new FileStream(Path.Combine(folder, reader.FileInfo.FileName), FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        reader.Read(writer);
                    }
                    break;

                case tar_cs.EntryType.HardLink:
                    if (reader.FileInfo.LinkName.StartsWith(DedupPrefix))
                    {
                        // This file has been deduplicated; resolve the actual file
                        // using the dictionary.
                        var hash = reader.FileInfo.LinkName.Substring(DedupPrefix.Length);
                        if (!hashesToStreams.ContainsKey(hash))
                        {
                            Console.WriteLine("WARNING: Unable to find deduplicated stream for file hash " + hash);
                        }
                        else
                        {
                            using (var writer = new FileStream(Path.Combine(folder, reader.FileInfo.FileName), FileMode.Create, FileAccess.Write, FileShare.None))
                            {
                                hashesToStreams[hash].CopyTo(writer);
                                hashesToStreams[hash].Seek(0, SeekOrigin.Begin);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("WARNING: Unknown hard link present in TAR archive.");
                    }
                    break;

                case tar_cs.EntryType.Directory:
                    Directory.CreateDirectory(Path.Combine(folder, reader.FileInfo.FileName));
                    break;

                default:
                    Console.WriteLine("WARNING: Ignoring unknown entry type in TAR archive.");
                    break;
                }
            }
        }