-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArchiver.cs
More file actions
197 lines (173 loc) · 8.47 KB
/
Archiver.cs
File metadata and controls
197 lines (173 loc) · 8.47 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#region Related components
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using SharpCompress.Common;
using SharpCompress.Archives;
using SharpCompress.Archives.Tar;
using SharpCompress.Writers.Tar;
#endregion
namespace net.vieapps.Components.Utility
{
/// <summary>
/// Servicing methods for working with archiver (Zip/Tar)
/// </summary>
public static partial class ArchivingService
{
/// <summary>
/// Archives a collection of files using ZIP
/// </summary>
/// <param name="files"></param>
/// <param name="archiveFilePath"></param>
/// <param name="compressionLevel"></param>
/// <param name="encoding"></param>
public static void Zip(IEnumerable<FileInfo> files, string archiveFilePath, CompressionLevel compressionLevel = CompressionLevel.Optimal, Encoding encoding = null)
{
if (files == null || !files.Any())
throw new ArgumentException("Source files are invalid", nameof(files));
if (string.IsNullOrWhiteSpace(archiveFilePath))
throw new ArgumentException("Path of .ZIP file is invalid", nameof(archiveFilePath));
if (File.Exists(archiveFilePath))
File.Delete(archiveFilePath);
using (var archiver = ZipFile.Open(archiveFilePath, ZipArchiveMode.Create, encoding ?? Encoding.UTF8))
files.Where(file => file.Exists).ForEach(file => archiver.CreateEntryFromFile(file.FullName, file.Name, compressionLevel));
}
/// <summary>
/// Archives a collection of files using ZIP
/// </summary>
/// <param name="files"></param>
/// <param name="archiveFilePath"></param>
/// <param name="compressionLevel"></param>
/// <param name="encoding"></param>
/// <param name="cancellationToken"></param>
public static Task ZipAsync(IEnumerable<FileInfo> files, string archiveFilePath, CompressionLevel compressionLevel = CompressionLevel.Optimal, Encoding encoding = null, CancellationToken cancellationToken = default)
=> UtilityService.ExecuteTask(() => ArchivingService.Zip(files, archiveFilePath, compressionLevel, encoding), cancellationToken);
/// <summary>
/// Archives a collection of files using ZIP
/// </summary>
/// <param name="files"></param>
/// <param name="archiveFilePath"></param>
/// <param name="compressionLevel"></param>
/// <param name="encoding"></param>
public static void Zip(IEnumerable<string> files, string archiveFilePath, CompressionLevel compressionLevel = CompressionLevel.Optimal, Encoding encoding = null)
{
if (files == null || !files.Any())
throw new ArgumentException("Source files are invalid", nameof(files));
if (string.IsNullOrWhiteSpace(archiveFilePath))
throw new ArgumentException("Path of .ZIP file is invalid", nameof(archiveFilePath));
ArchivingService.Zip(files.Select(path => new FileInfo(path)), archiveFilePath, compressionLevel, encoding);
}
/// <summary>
/// Archives a collection of files using ZIP
/// </summary>
/// <param name="files"></param>
/// <param name="archiveFilePath"></param>
/// <param name="compressionLevel"></param>
/// <param name="encoding"></param>
/// <param name="cancellationToken"></param>
public static Task ZipAsync(IEnumerable<string> files, string archiveFilePath, CompressionLevel compressionLevel = CompressionLevel.Optimal, Encoding encoding = null, CancellationToken cancellationToken = default)
=> UtilityService.ExecuteTask(() => ArchivingService.Zip(files, archiveFilePath, compressionLevel, encoding), cancellationToken);
/// <summary>
/// Archives a directory (means all files) using ZIP
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="archiveFilePath"></param>
/// <param name="compressionLevel"></param>
/// <param name="encoding"></param>
public static void Zip(string sourcePath, string archiveFilePath, CompressionLevel compressionLevel = CompressionLevel.Optimal, Encoding encoding = null)
{
if (!Directory.Exists(sourcePath))
throw new ArgumentException("Source path to the directory for archiving is invalid", nameof(sourcePath));
if (string.IsNullOrWhiteSpace(archiveFilePath))
throw new ArgumentException("Path of .ZIP file is invalid", nameof(archiveFilePath));
if (File.Exists(archiveFilePath))
File.Delete(archiveFilePath);
ZipFile.CreateFromDirectory(sourcePath, archiveFilePath, compressionLevel, false, encoding ?? Encoding.UTF8);
}
/// <summary>
/// Archives a directory (means all files) using ZIP
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="archiveFilePath"></param>
/// <param name="compressionLevel"></param>
/// <param name="encoding"></param>
/// <param name="cancellationToken"></param>
public static Task ZipAsync(string sourcePath, string archiveFilePath, CompressionLevel compressionLevel = CompressionLevel.Optimal, Encoding encoding = null, CancellationToken cancellationToken = default)
=> UtilityService.ExecuteTask(() => ArchivingService.Zip(sourcePath, archiveFilePath, compressionLevel, encoding), cancellationToken);
/// <summary>
/// UnArchives to a directory using ZIP
/// </summary>
/// <param name="archiveFilePath"></param>
/// <param name="destinationPath"></param>
public static void Unzip(string archiveFilePath, string destinationPath)
{
if (string.IsNullOrWhiteSpace(archiveFilePath))
throw new ArgumentException("Path of .ZIP file is invalid", nameof(archiveFilePath));
if (!Directory.Exists(destinationPath))
throw new ArgumentException("Destination path to the directory for unarchiving is invalid", nameof(destinationPath));
ZipFile.ExtractToDirectory(archiveFilePath, destinationPath);
}
/// <summary>
/// UnArchives to a directory using ZIP
/// </summary>
/// <param name="archiveFilePath"></param>
/// <param name="destinationPath"></param>
/// <param name="cancellationToken"></param>
public static Task UnzipAsync(string archiveFilePath, string destinationPath, CancellationToken cancellationToken = default)
=> UtilityService.ExecuteTask(() => ArchivingService.Unzip(archiveFilePath, destinationPath), cancellationToken);
/// <summary>
/// Archives a directory (means all files) using TAR/ZSTD
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="archiveFilePath"></param>
/// <param name="includeSubDirectories"></param>
/// <param name="compressionLevel"></param>
/// <exception cref="ArgumentException"></exception>
public static void Tar(string sourcePath, string archiveFilePath, bool includeSubDirectories = true, int compressionLevel = 10)
{
if (!Directory.Exists(sourcePath))
throw new ArgumentException("Source path to the directory for archiving is invalid", nameof(sourcePath));
var files = Directory.EnumerateFiles(sourcePath, "*.*", includeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.OrderBy(filePath =>
{
var ext = Path.GetExtension(filePath).ToLowerInvariant();
return ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".webp" || ext == ".mp4" || ext == ".mkv" || ext == ".avi" || ext == ".zip" || ext == ".rar" || ext == ".7z" ? 1 : 0;
})
.Select(filePath => new FileInfo(filePath))
.ToList();
using (var fileStream = File.Create(archiveFilePath))
using (var zstdStream = new ZstdSharp.CompressionStream(fileStream, level: compressionLevel, leaveOpen: false))
using (var archiver = TarArchive.CreateArchive())
{
files.ForEach(file => archiver.AddEntry(file.Name, file.FullName));
archiver.SaveTo(zstdStream, new TarWriterOptions(CompressionType.None));
}
}
/// <summary>
/// UnArchives to a directory using TAR/ZSTD
/// </summary>
/// <param name="archiveFilePath"></param>
/// <param name="destinationPath"></param>
/// <exception cref="ArgumentException"></exception>
public static void UnTar(string archiveFilePath, string destinationPath)
{
if (string.IsNullOrWhiteSpace(archiveFilePath))
throw new ArgumentException("Path of .TAR.ZST file is invalid", nameof(archiveFilePath));
if (!Directory.Exists(destinationPath))
throw new ArgumentException("Destination path to the directory for unarchiving is invalid", nameof(destinationPath));
using (var fileStream = File.OpenRead(archiveFilePath))
using (var zstdStream = new ZstdSharp.DecompressionStream(fileStream))
using (var archiver = TarArchive.OpenArchive(zstdStream))
foreach (var entry in archiver.Entries)
{
if (!entry.IsDirectory)
entry.WriteToDirectory(destinationPath);
}
}
}
}