forked from bloom-housing/bloom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release: refactor and extend zip code (bloom-housing#4588) (#1062)
- Loading branch information
Showing
9 changed files
with
154 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { StreamableFile } from '@nestjs/common'; | ||
import archiver from 'archiver'; | ||
import fs, { createReadStream, ReadStream } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
/** | ||
* | ||
* @param readStream | ||
* @param zipFilename | ||
* @param filename | ||
* @param isSpreadsheet | ||
* @returns a promise containing a streamable file | ||
*/ | ||
export const zipExport = ( | ||
readStream: ReadStream, | ||
zipFilename: string, | ||
filename: string, | ||
isSpreadsheet: boolean, | ||
): Promise<StreamableFile> => { | ||
const zipFilePath = join(process.cwd(), `src/temp/${zipFilename}.zip`); | ||
|
||
return new Promise((resolve) => { | ||
// Create a writable stream to the zip file | ||
const output = fs.createWriteStream(zipFilePath); | ||
const archive = archiver('zip', { | ||
zlib: { level: 9 }, | ||
}); | ||
output.on('close', () => { | ||
const zipFile = createReadStream(zipFilePath); | ||
resolve(new StreamableFile(zipFile)); | ||
}); | ||
|
||
archive.pipe(output); | ||
archive.append(readStream, { | ||
name: `${filename}.${isSpreadsheet ? 'xlsx' : 'csv'}`, | ||
}); | ||
archive.finalize(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.