-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
Zip To Memory | ||
# Zip In Memory | ||
|
||
Dragon: "My name is Balthromaw. Breaker of skies, slayer of mountain." | ||
Rick: "Rule 1: You're now scooper of your own poops, or I will take you down like the black-light poster you are." | ||
|
||
![Stonks](https://i.kym-cdn.com/photos/images/newsfeed/001/499/826/2f0.png) | ||
|
||
#### From Bezunca Labs |
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,72 @@ | ||
package zip_in_memory | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"io/ioutil" | ||
) | ||
|
||
type EmptyZipFileError struct {} | ||
|
||
func (e *EmptyZipFileError) Error() string { | ||
return "empty zip file" | ||
} | ||
|
||
func ExtractFirstFile(data []byte) ([]byte, error){ | ||
readerAt := bytes.NewReader(data) | ||
zipReader, err := zip.NewReader(readerAt, int64(len(data))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(zipReader.File) > 0 { | ||
file, err := zipReader.File[0].Open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
extractedData, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := file.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return extractedData, nil | ||
} | ||
|
||
return nil, &EmptyZipFileError{} | ||
} | ||
|
||
func ExtractFiles(data []byte) ([][]byte, error){ | ||
readerAt := bytes.NewReader(data) | ||
zipReader, err := zip.NewReader(readerAt, int64(len(data))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
files := make([][]byte, 0, len(zipReader.File)) | ||
for _, f := range zipReader.File { | ||
file, err := f.Open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
extractedData, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := file.Close(); err != nil { | ||
return nil, err | ||
} | ||
|
||
files = append(files, extractedData) | ||
} | ||
|
||
if len(files) == 0 { | ||
return nil, &EmptyZipFileError{} | ||
} | ||
|
||
return files, nil | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/Bezunca/ZipInMemory | ||
module github.com/Bezunca/zip_in_memory | ||
|
||
go 1.14 | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.