BadScript 2
Loading...
Searching...
No Matches
BadFileSystemHelper.cs
Go to the documentation of this file.
1using System.IO.Compression;
2using System.Text;
5
6namespace BadScript2.IO;
7
11public static class BadFileSystemHelper
12{
20 public static void ExportZip(this IFileSystem fs, Stream str, string path = "/")
21 {
22 BadLogger.Log("Exporting zip file..", "BFS");
23
24 if (fs is not BadVirtualFileSystem vfs)
25 {
26 throw new NotSupportedException("Only virtual file systems are supported");
27 }
28
29 using ZipArchive zip = new ZipArchive(str, ZipArchiveMode.Update, true);
30
31 foreach (string file in vfs.GetFiles(path, "", true))
32 {
33 BadLogger.Log("Exporting File: " + file, "BFS");
34 ZipArchiveEntry e = zip.CreateEntry(file.Remove(0, path.Length));
35 using Stream estr = e.Open();
36 using Stream fstr = vfs.OpenRead(file);
37 fstr.CopyTo(estr);
38 }
39 }
40
41 public static void ImportZip(this IFileSystem fs, string path, string root = "/")
42 {
43 using FileStream str = new FileStream(path, FileMode.Open, FileAccess.Read);
44 fs.ImportZip(str, root);
45 }
46
55 public static void ImportZip(this IFileSystem fs, Stream str, string root = "/", Encoding? encoding = null)
56 {
57 BadLogger.Log("Importing zip file..", "BFS");
58
59 if (fs is not BadVirtualFileSystem vfs)
60 {
61 throw new NotSupportedException("Only virtual file systems are supported");
62 }
63
64 ZipArchive? arch = new ZipArchive(str, ZipArchiveMode.Read, false, encoding ?? Encoding.UTF8);
65
66 if (arch == null)
67 {
68 throw new Exception("Failed to open zip file");
69 }
70
71 foreach (ZipArchiveEntry entry in arch.Entries)
72 {
73 BadLogger.Log("Importing File: " + entry.FullName, "BFS");
74
75 if (BadVirtualPathReader.IsDirectory(root + entry.FullName))
76 {
77 continue;
78 }
79
80 string? dir = Path.GetDirectoryName(entry.FullName);
81
82 if (dir != null && !string.IsNullOrEmpty(dir))
83 {
84 vfs.CreateDirectory(root + dir, true);
85 }
86
87 using Stream s = entry.Open();
88 using Stream o = vfs.OpenWrite(root + entry.FullName, BadWriteMode.CreateNew);
89 s.CopyTo(o);
90 }
91
92 arch.Dispose();
93 }
94}
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 ExportZip(this IFileSystem fs, Stream str, string path="/")
Exports a Virtual File System to a Zip File.
static void ImportZip(this IFileSystem fs, Stream str, string root="/", Encoding? encoding=null)
Imports a Zip File to a Virtual File System.
static void ImportZip(this IFileSystem fs, string path, string root="/")
Virtual File System Implementation for the BadScript Engine.
Implements Operations to read and manipulate File System Paths.
Defines the interface for a file system.
Definition IFileSystem.cs:7
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.