chore(HM):使解压功能可用

This commit is contained in:
Tianpao
2026-01-01 00:26:14 +08:00
parent 81ae7bec50
commit c9a7b6f303
9 changed files with 278 additions and 142 deletions

View File

@@ -1,6 +1,7 @@
import fs from "node:fs"
import crypto from "node:crypto"
import { yauzl_promise } from "./yauzl.promise.js"
//import { yauzl_promise } from "./yauzl.promise.js"
import { Azip } from "./ziplib.js"
import got from "got"
import { Utils } from "./utils.js"
import config from "./config.js"
@@ -56,6 +57,7 @@ export class DeEarth{
const result = [...new Set(hash.concat(mixins))]
//console.log(result)
result.forEach(async e=>{
console.log(e)
await fs.promises.rename(`${e}`,`${this.movepath}/${e}`.replace(this.modspath,""))
//await fs.promises.rename(`${this.modspath}/${e}`,`${this.movepath}/${e}`)
})
@@ -136,9 +138,9 @@ export class DeEarth{
const data = fs.readFileSync(file)
const sha1 = crypto.createHash('sha1').update(data).digest('hex') //Get Hash
const mxarr:{name:string,data:string}[] = []
const mixins = (await yauzl_promise(data)).forEach(async e=>{ //Get Mixins Info to check
if(e.fileName.endsWith(".mixins.json")&&!e.fileName.includes("/")){
mxarr.push({name:e.fileName,data:(await e.ReadEntry).toString()})
const mixins = (Azip(data)).forEach(async e=>{ //Get Mixins Info to check
if(e.entryName.endsWith(".mixins.json")&&!e.entryName.includes("/")){
mxarr.push({name:e.entryName,data:(await e.getData()).toString()})
}
})
arr.push({filename:file,hash:sha1,mixins:mxarr})

View File

@@ -1,72 +0,0 @@
import yauzl from "yauzl";
import Stream from "node:stream"
export interface IentryP extends yauzl.Entry {
openReadStream: Promise<Stream.Readable>;
ReadEntry: Promise<Buffer>;
}
export async function yauzl_promise(buffer: Buffer): Promise<IentryP[]>{
const zip = await (new Promise((resolve,reject)=>{
yauzl.fromBuffer(buffer,/*{lazyEntries:true},*/ (err, zipfile) => {
if (err){
reject(err);
return;
}
resolve(zipfile);
});
return;
}) as Promise<yauzl.ZipFile>);
return new Promise((resolve, reject) => {
const entries: IentryP[]= []
zip.on("entry", async (entry: yauzl.Entry) => {
const _entry = {
...entry,
getLastModDate: entry.getLastModDate,
isEncrypted: entry.isEncrypted,
isCompressed: entry.isCompressed,
openReadStream: _openReadStream(zip,entry),
ReadEntry: _ReadEntry(zip,entry),
}
entries.push(_entry)
if (zip.entryCount === entries.length){
zip.close();
resolve(entries);
}
});
zip.on("error",err=>{
reject(err);
})
});
}
async function _ReadEntry(zip:yauzl.ZipFile,entry:yauzl.Entry): Promise<Buffer>{
return new Promise((resolve,reject)=>{
zip.openReadStream(entry,(err,stream)=>{
if (err){
reject(err);
return;
}
const chunks: Buffer[] = [];
stream.on("data",(chunk)=>{
chunks.push(chunk);
})
stream.on("end",()=>{
resolve(Buffer.concat(chunks));
})
})
})
}
async function _openReadStream(zip:yauzl.ZipFile,entry:yauzl.Entry): Promise<Stream.Readable>{
return new Promise((resolve,reject)=>{
zip.openReadStream(entry,(err,stream)=>{
if (err){
reject(err);
return;
}
resolve(stream);
})
})
}

View File

@@ -0,0 +1,89 @@
import yauzl from "yauzl";
import Stream from "node:stream"
export interface IentryP extends yauzl.Entry {
openReadStream: Promise<Stream.Readable>;
ReadEntry: Promise<Buffer>;
}
export async function yauzl_promise(buffer: Buffer): Promise<IentryP[]>{
const zip = await (new Promise((resolve,reject)=>{
yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipfile) => {
if (err){
reject(err);
return;
}
resolve(zipfile);
});
}) as Promise<yauzl.ZipFile>);
return await new Promise((resolve, reject) => {
const entries: IentryP[] = [];
let entryCount = 0;
zip.on("entry", (entry: yauzl.Entry) => {
// 创建新对象并复制所有entry属性避免yauzl重用对象导致的引用问题
const _entry = Object.assign({}, entry) as IentryP;
_entry.openReadStream = _openReadStream(zip, entry);
_entry.ReadEntry = _ReadEntry(zip, entry);
entries.push(_entry);
entryCount++;
//console.log(entryCount, entry.fileName);
// 继续读取下一个条目
zip.readEntry();
});
zip.on("end", () => {
zip.close();
console.log(entryCount, "entries read");
if(entryCount === zip.entryCount){
console.log("All entries read");
resolve(entries);
}
});
zip.on("error", (err) => {
reject(err);
});
// 开始读取第一个条目
zip.readEntry();
});
}
async function _openReadStream(zip: yauzl.ZipFile, entry: yauzl.Entry): Promise<Stream.Readable>{
return new Promise((resolve, reject) => {
zip.openReadStream(entry, (err, stream) => {
if (err) {
reject(err);
return;
}
resolve(stream);
});
});
}
async function _ReadEntry(zip: yauzl.ZipFile, entry: yauzl.Entry): Promise<Buffer>{
return new Promise((resolve, reject) => {
zip.openReadStream(entry, (err, stream) => {
if (err) {
reject(err);
return;
}
const chunks: Buffer[] = [];
stream.on("data", (chunk) => {
chunks.push(chunk);
});
stream.on("end", () => {
resolve(Buffer.concat(chunks));
});
stream.on("error", (err) => {
reject(err);
});
});
});
}

View File

@@ -0,0 +1,84 @@
import admZip from "adm-zip";
import yauzl from "yauzl";
import Stream from "node:stream";
export interface IentryP extends yauzl.Entry {
openReadStream: Promise<Stream.Readable>;
ReadEntry: Promise<Buffer>;
}
export async function yauzl_promise(buffer: Buffer): Promise<IentryP[]> {
const zip = await (new Promise((resolve, reject) => {
yauzl.fromBuffer(
buffer,
/*{lazyEntries:true},*/ (err, zipfile) => {
if (err) {
reject(err);
return;
}
resolve(zipfile);
}
);
return;
}) as Promise<yauzl.ZipFile>);
const _ReadEntry = async (
zip: yauzl.ZipFile,
entry: yauzl.Entry
): Promise<Buffer> => {
return new Promise((resolve, reject) => {
zip.openReadStream(entry, (err, stream) => {
if (err) {
reject(err);
return;
}
const chunks: Buffer[] = [];
stream.on("data", (chunk) => {
chunks.push(chunk);
});
stream.on("end", () => {
resolve(Buffer.concat(chunks));
});
});
});
};
const _openReadStream = async (
zip: yauzl.ZipFile,
entry: yauzl.Entry
): Promise<Stream.Readable> => {
return new Promise((resolve, reject) => {
zip.openReadStream(entry, (err, stream) => {
if (err) {
reject(err);
return;
}
resolve(stream);
});
});
};
return new Promise((resolve, reject) => {
const entries: IentryP[] = [];
zip.on("entry", async (entry: yauzl.Entry) => {
const entryP = entry as IentryP;
//console.log(entry.fileName);
entryP.openReadStream = _openReadStream(zip, entry);
entryP.ReadEntry = _ReadEntry(zip, entry);
entries.push(entryP);
if (zip.entryCount === entries.length) {
zip.close();
resolve(entries);
}
});
zip.on("error", (err) => {
reject(err);
});
});
}
export function Azip(buffer: Buffer) {
const zip = new admZip(buffer);
const entries = zip.getEntries();
return entries;
}