| 4 | import { ZipFileReader, ZipFileWriter } from "./rw"; |
| 5 | |
| 6 | export default class ZipFileSystem implements FileSystem { |
| 7 | zip: JSZipFile; |
| 8 | |
| 9 | basePath: string; |
| 10 | |
| 11 | // zip为空时,创建一个空的zip |
| 12 | constructor(zip: JSZipFile, basePath?: string) { |
| 13 | this.zip = zip; |
| 14 | this.basePath = basePath || ""; |
| 15 | } |
| 16 | |
| 17 | async verify(): Promise<void> { |
| 18 | // do nothing |
| 19 | } |
| 20 | |
| 21 | async open(info: FileInfo): Promise<FileReader> { |
| 22 | const path = info.name; |
| 23 | const file = this.zip.file(path); |
| 24 | if (file) { |
| 25 | return new ZipFileReader(file); |
| 26 | } |
| 27 | throw new Error("File not found"); |
| 28 | } |
| 29 | |
| 30 | async openDir(path: string): Promise<FileSystem> { |
| 31 | return new ZipFileSystem(this.zip, path); |
| 32 | } |
| 33 | |
| 34 | async create(path: string, opts?: FileCreateOptions): Promise<FileWriter> { |
| 35 | return new ZipFileWriter(this.zip, path, opts); |
| 36 | } |
| 37 | |
| 38 | async createDir(_path: string, _opts?: FileCreateOptions): Promise<void> { |
| 39 | // do nothing |
| 40 | } |
| 41 | |
| 42 | async delete(path: string): Promise<void> { |
| 43 | this.zip.remove(path); |
| 44 | } |
| 45 | |
| 46 | async list(): Promise<FileInfo[]> { |
| 47 | const files: FileInfo[] = []; |
| 48 | for (const [filename, jsZipObject] of Object.entries(this.zip.files)) { |
| 49 | const date = jsZipObject.date; // the last modification date |
| 50 | const lastModificationDate = date.getTime(); |
| 51 | files.push({ |
| 52 | name: filename, |
| 53 | path: filename, |
| 54 | size: 0, |
| 55 | digest: "", |
| 56 | createtime: lastModificationDate, |
| 57 | updatetime: lastModificationDate, |
| 58 | }); |
| 59 | } |
| 60 | return files; |
| 61 | } |
| 62 | |
| 63 | async getDirUrl(): Promise<string> { |
nothing calls this directly
no outgoing calls
no test coverage detected