Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

压缩后文件的修改时间不正确 #665

Open
YSHIDM opened this issue Jul 5, 2023 · 0 comments
Open

压缩后文件的修改时间不正确 #665

YSHIDM opened this issue Jul 5, 2023 · 0 comments

Comments

@YSHIDM
Copy link

YSHIDM commented Jul 5, 2023

代码示例:

// ### get excel
const ExcelJS = require('exceljs');
const archiver = require('archiver');
const path = require('path');
const fs = require('fs');

/**
 * 获取excel 内容
 * @returns {Promise<ExcelJS.Workbook>}
 */
async function getExcel() {
  let now = new Date();
  let sheet_name = `export(${now.toDateString()})`;
  const workbook = new ExcelJS.Workbook();
  workbook.creator = 'Me';
  workbook.lastModifiedBy = 'Her';
  workbook.created = now;
  workbook.modified = now;
  workbook.lastPrinted = now;

  workbook.addWorksheet(sheet_name, {});
  return workbook;
}
/**
 * 保存excel
 * @param {*} excelDir excel 位置
 * @param {*} workbook excel 内容
 * @param {string} excelName excel 名称
 * @returns
 */
async function storeExcel(excelDir, workbook, excelName = 'test.xlsx') {
  let fpath = path.join(excelDir, excelName);
  workbook.worksheets[0].views = [
    { state: 'frozen', xSplit: 1, ySplit: 1, topLeftCell: 'A1', activeCell: 'A1' }
  ];
  await workbook.xlsx.writeFile(fpath).catch(err => console.error(err));
  return fpath;
}

async function createZip(excelDir) {
  const output = fs.createWriteStream(__dirname + '/example.zip');
  const archive = archiver('zip', {
    zlib: { level: 9 }
  });
  archive.pipe(output);
  let excelBuffer = await fs.promises.readFile(excelDir);
  if (excelBuffer) {
    const excelName = path.basename(excelDir)
    archive.append(excelBuffer, { name: excelName });
  }

  await archive.finalize();
}
async function test() {
  let workbook = await getExcel();
  let store_dir = __dirname;
  let excelPath = await storeExcel(store_dir, workbook);
  console.log('excelPath :>>', excelPath)
  await createZip(excelPath)
}
test()

结果如图:
image
压缩后修改时间不是源文件修改时间,也不是中国时区当前时间

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant