File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ // Import JSZip if using module-based JavaScript (for npm installs)
2+ import JSZip from "jszip" ;
3+
4+ // Function to unzip a file
5+ async function unzipFile ( file ) {
6+ const zip = new JSZip ( ) ;
7+
8+ try {
9+ // Load the zip file
10+ const loadedZip = await zip . loadAsync ( file ) ;
11+
12+ // Extract each file
13+ const extractedFiles = { } ;
14+ await Promise . all (
15+ Object . keys ( loadedZip . files ) . map ( async ( filename ) => {
16+ const content = await loadedZip . files [ filename ] . async ( "string" ) ; // 'string' or 'blob' based on your need
17+ extractedFiles [ filename ] = content ;
18+ } )
19+ ) ;
20+
21+ console . log ( "Extracted Files:" , extractedFiles ) ;
22+ return extractedFiles ;
23+ } catch ( err ) {
24+ console . error ( "Error unzipping file:" , err ) ;
25+ return null ;
26+ }
27+ }
28+
29+ // Usage example:
30+ // Assuming `fileInput` is a file input element
31+ document . getElementById ( 'fileInput' ) . addEventListener ( 'change' , async ( event ) => {
32+ const file = event . target . files [ 0 ] ;
33+ if ( file ) {
34+ const unzippedContent = await unzipFile ( file ) ;
35+ console . log ( unzippedContent ) ;
36+ }
37+ } ) ;
38+
You can’t perform that action at this time.
0 commit comments