BadScript 2
Loading...
Searching...
No Matches
BadFileSystemHelper.cs
Go to the documentation of this file.
1using System.IO.Compression;
2using System.Text;
3
6
7namespace BadScript2.IO;
8
12public static class BadFileSystemHelper
13{
21 public static void ExportZip(this IVirtualFileSystem fs, Stream str, string path = "/")
22 {
23 BadLogger.Log("Exporting zip file..", "BFS");
24
25 using ZipArchive zip = new ZipArchive(str, ZipArchiveMode.Update, true);
26
27 foreach (string file in fs.GetFiles(path, "", true))
28 {
29 BadLogger.Log("Exporting File: " + file, "BFS");
30 ZipArchiveEntry e = zip.CreateEntry(file.Remove(0, path.Length));
31 using Stream estr = e.Open();
32 using Stream fstr = fs.OpenRead(file);
33 fstr.CopyTo(estr);
34 }
35 }
36
37 public static void ImportZip(this IVirtualFileSystem fs, string path, string root = "/")
38 {
39 using FileStream str = new FileStream(path, FileMode.Open, FileAccess.Read);
40 fs.ImportZip(str, root);
41 }
42
51 public static void ImportZip(this IVirtualFileSystem fs, Stream str, string root = "/", Encoding? encoding = null)
52 {
53 BadLogger.Log("Importing zip file..", "BFS");
54
55
56 ZipArchive? arch = new ZipArchive(str, ZipArchiveMode.Read, false, encoding ?? Encoding.UTF8);
57
58 if (arch == null)
59 {
60 throw new Exception("Failed to open zip file");
61 }
62
63 foreach (ZipArchiveEntry entry in arch.Entries)
64 {
65 BadLogger.Log("Importing File: " + entry.FullName, "BFS");
66
67 if (BadVirtualPathReader.IsDirectory(root + entry.FullName))
68 {
69 continue;
70 }
71
72 string? dir = Path.GetDirectoryName(entry.FullName);
73
74 if (dir != null && !string.IsNullOrEmpty(dir))
75 {
76 fs.CreateDirectory(root + dir, true);
77 }
78
79 using Stream s = entry.Open();
80 using Stream o = fs.OpenWrite(root + entry.FullName, BadWriteMode.CreateNew);
81 s.CopyTo(o);
82 }
83
84 arch.Dispose();
85 }
86}
Public facing interface for a logger.
Definition BadLogger.cs:7
static void Log(string message)
Writes a Log to the Message Handler.
Definition BadLogger.cs:26
File System Helper Extensions.
static void ImportZip(this IVirtualFileSystem fs, Stream str, string root="/", Encoding? encoding=null)
Imports a Zip File to a Virtual File System.
static void ExportZip(this IVirtualFileSystem fs, Stream str, string path="/")
Exports a Virtual File System to a Zip File.
static void ImportZip(this IVirtualFileSystem fs, string path, string root="/")
Implements Operations to read and manipulate File System Paths.
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6
Contains the Implementation of the BadScript Virtual File System.
Contains IO Implementation for the BadScript2 Runtime.
BadWriteMode
The Write Modes of the File System Abstraction.