예제 #1
0
        private void ExtractTarEntry(TarReader tarReader, string outputDirectory, string packName, AsyncFileWriteQueue smallFileQueue, int smallFileSize)
        {
            var relativePath = tarReader.FileInfo.FileName;

            if (relativePath.StartsWith("package/"))
            {
                relativePath = relativePath.Substring("package/".Length);
            }
            else if (relativePath.StartsWith(packName + "/"))
            {
                relativePath = relativePath.Substring((packName + "/").Length);
            }

            // Relative path can contain slash, not backslash.
            // Use Path.GetFullPath() method to convert path.
            var fullPath = Path.GetFullPath(Path.Combine(outputDirectory, relativePath));

            switch (tarReader.FileInfo.EntryType)
            {
            case EntryType.File:
            case EntryType.FileObsolete:

                var file = new FileInfo(fullPath);

                if (!_createdDirs.Contains(file.DirectoryName))
                {
                    _dirsCreated++;
                    file.Directory.Create();
                    _createdDirs.Add(file.DirectoryName);
                }

                if (smallFileQueue == null || tarReader.FileInfo.SizeInBytes > smallFileQueue.MaxSize || tarReader.FileInfo.SizeInBytes > smallFileSize)
                {
                    using (var outputStream = File.Create(fullPath))
                    {
                        _filesCreated++;
                        // Read data from a current file to a Stream.

                        tarReader.Read(outputStream);
                    }
                }
                else
                {
                    using (var outputStream = new MemoryStream())
                    {
                        _filesCreated++;

                        tarReader.Read(outputStream);

                        smallFileQueue.Enqueue(fullPath, outputStream.ToArray(), TimeSpan.FromMinutes(1)).Wait();
                    }
                }


                break;

            case EntryType.Directory:
                Directory.CreateDirectory(fullPath);
                break;

            default:
                break;
                //     throw new NotSupportedException("Not supported entry type: " + tarReader.FileInfo.EntryType);
            }
        }