aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Archiving/ZipClient.cs
blob: 6a3b250d254c6cf79e684aae4764ead98019d367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.IO;
using MediaBrowser.Model.IO;
using SharpCompress.Common;
using SharpCompress.Readers;
using SharpCompress.Readers.GZip;

namespace Emby.Server.Implementations.Archiving
{
    /// <summary>
    /// Class DotNetZipClient.
    /// </summary>
    public class ZipClient : IZipClient
    {
        /// <inheritdoc />
        public void ExtractAllFromGz(Stream source, string targetPath, bool overwriteExistingFiles)
        {
            using var reader = GZipReader.Open(source);
            var options = new ExtractionOptions
            {
                ExtractFullPath = true,
                Overwrite = overwriteExistingFiles
            };

            Directory.CreateDirectory(targetPath);
            reader.WriteAllToDirectory(targetPath, options);
        }

        /// <inheritdoc />
        public void ExtractFirstFileFromGz(Stream source, string targetPath, string defaultFileName)
        {
            using var reader = GZipReader.Open(source);
            if (reader.MoveToNextEntry())
            {
                var entry = reader.Entry;

                var filename = entry.Key;
                if (string.IsNullOrWhiteSpace(filename))
                {
                    filename = defaultFileName;
                }

                reader.WriteEntryToFile(Path.Combine(targetPath, filename));
            }
        }
    }
}