-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
160 lines (132 loc) · 4.88 KB
/
Program.cs
File metadata and controls
160 lines (132 loc) · 4.88 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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace har2zip
{
class Program
{
static void Main(string[] args)
{
var filename = string.Join(" ", args);
string content = null;
try {
content = File.ReadAllText(filename);
}
catch (Exception ex) {
Console.WriteLine("Unable to open file: " + filename);
Console.WriteLine(ex.Message);
Environment.Exit(1);
}
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
HarFile har = null;
try {
har = JsonSerializer.Deserialize<HarFile>(content, options);
}
catch (Exception ex) {
Console.WriteLine("Unable to deserialize file content: " + filename);
Console.WriteLine(ex.Message);
Environment.Exit(2);
}
List<InMemoryFile> files = har.Log?.Entries?
.Where(x => x?.Response?.BodySize > 0)
.Select(x => ToInMemoryFile(x))
.GroupBy(x => x.FileName)
.Select(x => x.First())
.ToList();
try {
var zip = GetZipArchive(files);
File.WriteAllBytes(filename + ".zip", zip);
}
catch (Exception ex) {
Console.WriteLine("Unable to create zip file from files:");
Console.WriteLine(ex.Message);
Environment.Exit(4);
}
}
public static InMemoryFile ToInMemoryFile(HarEntry entry) {
try {
var fileUri = new Uri(entry.Request.Url);
var filename = $"{fileUri.Host}{fileUri.LocalPath}";
if (!string.IsNullOrEmpty(fileUri.Query)) {
var path = Path.GetExtension(fileUri.LocalPath);
filename += $"{fileUri.Query}{path}";
}
var content = Unpack(entry.Response.Content);
var memfile = new InMemoryFile() {
FileName = Friendly(filename),
Content = content
};
return memfile;
}
catch (Exception ex) {
Console.WriteLine("Unable to create in-memory file from entry: ");
Console.WriteLine(JsonSerializer.Serialize(entry));
Console.WriteLine(ex.Message);
Environment.Exit(3);
}
return null;
}
private static byte[] Unpack(HarContent content) {
if (content.Encoding?.ToLowerInvariant() == "base64") {
return Convert.FromBase64String(content.Text);
}
return Encoding.UTF8.GetBytes(content.Text);
}
private static string Friendly(string text) {
return new Regex("[^a-zA-Z0-9/._-]").Replace(text, "_");
}
public static byte[] GetZipArchive(List<InMemoryFile> files)
{
byte[] archiveFile;
using (var archiveStream = new MemoryStream())
{
using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
var zipArchiveEntry = archive.CreateEntry(file.FileName, CompressionLevel.Fastest);
using (var zipStream = zipArchiveEntry.Open())
zipStream.Write(file.Content, 0, file.Content.Length);
}
}
archiveFile = archiveStream.ToArray();
}
return archiveFile;
}
public class InMemoryFile
{
public string FileName { get; set; }
public byte[] Content { get; set; }
}
public class HarFile {
public HarLog Log { get; set; }
}
public class HarLog {
public List<HarEntry> Entries { get; set; }
}
public class HarEntry {
public HarRequest Request { get; set; }
public HarResponse Response { get; set; }
}
public class HarRequest {
public string Url { get; set; }
}
public class HarResponse {
public HarContent Content { get; set; }
public long BodySize { get; set; }
}
public class HarContent {
public string Text { get; set; }
public string Encoding { get; set; }
public string MimeType { get; set; }
}
}
}