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
        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. 3
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. 4
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. 5
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;
                }
            }
        }