@guanghechen/filesplit
v2.0.2
Published
File helper
Maintainers
Readme
A utility class for splitting large files into smaller chunks and merging them back. Inspired by node-split-file.
Install
npm
npm install --save @guanghechen/filesplityarn
yarn add @guanghechen/filesplit
Usage
Split a File
import { FileSplitter } from '@guanghechen/filesplit'
import { calcFilePartItemsBySize } from '@guanghechen/filepart'
import fs from 'node:fs'
async function splitFile(filepath: string): Promise<string[]> {
const splitter = new FileSplitter()
const fileSize = fs.statSync(filepath).size
const parts = [...calcFilePartItemsBySize(fileSize, 1024 * 1024 * 80)] // 80MB per chunk
const partFilepaths: string[] = await splitter.split(filepath, parts)
return partFilepaths
}
// Split 'large-file.zip' into 80MB chunks
// Result: ['large-file.zip.ghc-part01', 'large-file.zip.ghc-part02', ...]
await splitFile('large-file.zip')Merge Files
import { FileSplitter } from '@guanghechen/filesplit'
const splitter = new FileSplitter()
// Merge chunks back into original file
await splitter.merge(
['large-file.zip.ghc-part01', 'large-file.zip.ghc-part02', 'large-file.zip.ghc-part03'],
'large-file-restored.zip'
)Custom Part Code Prefix
import { FileSplitter } from '@guanghechen/filesplit'
// Use custom suffix for part files
const splitter = new FileSplitter({ partCodePrefix: '.chunk' })
// Split will create: file.txt.chunk01, file.txt.chunk02, etc.Using Default Instance
import { fileSplitter } from '@guanghechen/filesplit'
// Use the default singleton instance
await fileSplitter.split(filepath, parts)
await fileSplitter.merge(inputFilepaths, outputFilepath)Calculate Part Filepaths
import { FileSplitter } from '@guanghechen/filesplit'
import { calcFilePartItemsByCount } from '@guanghechen/filepart'
const splitter = new FileSplitter()
const parts = [...calcFilePartItemsByCount(1000000, 3)]
// Get the expected part filepaths without actually splitting
const partFilepaths = splitter.calcPartFilepaths('/path/to/file.zip', parts)
// Result: ['/path/to/file.zip.ghc-part1', '/path/to/file.zip.ghc-part2', '/path/to/file.zip.ghc-part3']