Skip to content

Instantly share code, notes, and snippets.

@Guestman360
Created May 29, 2023 18:49
Show Gist options
  • Select an option

  • Save Guestman360/e007f57e7097ff413ab2dcc63f90c8fb to your computer and use it in GitHub Desktop.

Select an option

Save Guestman360/e007f57e7097ff413ab2dcc63f90c8fb to your computer and use it in GitHub Desktop.
Code snippet for zipping and converting to base64
import fs from 'fs';
import * as path from 'path';
import shell from 'shelljs';
export const createZipWithBase64 = (user_id: string, code: string): string => {
fs.writeFileSync(`${path.join(__dirname, '/cpp/additional_files/main.cpp')}`, code);
// packaging in a zip archive and encoding in base64
shell.exec('base64 -v');
shell.exec('base64 --help');
shell.exec(
`cd ${path.join(__dirname, '/cpp/additional_files')} || exit; zip -r - . | base64 > ${path.resolve(
__dirname,
`../../zip_b64/${user_id}.txt`,
)}`,
);
// reading result base64 from a file
const zip_b64 = fs.readFileSync(`${path.resolve(__dirname, `../../zip_b64/${user_id}.txt`)}`).toString();
return zip_b64;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment