Comments (2)
Try something like this instead:
func extractFile(file *sevenzip.File, target string) error {
_f, err := os.OpenFile(target, os.O_CREATE, file.Mode())
if err != nil {
return err
}
defer _f.Close()
fReader, err := file.Open()
if err != nil {
return err
}
defer fReader.Close()
_, err = io.Copy(_f, fReader)
return err
}
func extract7zArchive(archive string, path string, password string) error {
reader, err := sevenz.OpenReaderWithPassword(archive, password)
if err != nil {
return err
}
defer reader.Close()
for _, f := range reader.File {
target := PathAppend(path, f.Name)
// seems no flag to check if f is directory, but it will end with / if is directory
if IsStrEndWith(f.Name, "/", true) {
if err := os.MkdirAll(target, f.Mode()); err != nil {
return err
}
} else {
pre := time.Now().UnixMilli()
if err = extractFile(f, target); err != nil {
return err
}
LogInfo("%d ms cost open %s", time.Now().UnixMilli()-pre, f.Name)
}
}
return nil
}
I've not compile-tested it but you should be able to get the idea.
The point is to close the io.ReadCloser
as soon as you finished extracting a file before extracting the next one. The way your code is currently structured means that all of the io.ReadClosers
are deferred until extract7zArchive()
completes before closing all of them.
Internally, to read file n
in an archive, you have to start at the beginning of the stream and read (and discard) the data for files 0 to n-1
, so for increasing values of n
it will get slower and slower as you have to read and discard more and more.
There's an optimisation in my library that will reuse the stream reader but that only works if you call Close()
on the file reader before opening the next one.
You're also doing the same with your filehandles returned from os.OpenFile()
so you can potentially run out of filehandles available to your process when dealing with very large archives.
from sevenzip.
Thanks a lot, I close reader before open next file item, it works now
from sevenzip.
Related Issues (20)
- runtime error: index out of range [0] with length 0 HOT 9
- "invalid memory address or nil pointer dereference" when opening 7z file HOT 1
- Errors are not accessible since they are private with a lower case
- Extract files from a self-extracting exe HOT 8
- panic: runtime error: index out of range HOT 2
- low performance for aes7z HOT 2
- warning: a function definition without a prototype is deprecated in all versions HOT 2
- 解带密码的提示 Failed to open file in archive: lzma: unsupported chunk header byte HOT 1
- How to extract HOT 1
- Return list of volume archive files. HOT 14
- seek over file in archive HOT 2
- Support BCJ method HOT 8
- Support PPC method
- Unable to decrypt 7-ZIP file with password (err: breader.ReadByte: no data!) HOT 1
- Unable to decrypt 7-ZIP file with password (err: breader.ReadByte: no data!) HOT 1
- sevenzip as a guide HOT 3
- Add support for reading self-extracting archives
- Empty File processing issue HOT 4
- Fail to read .exe in .7z HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sevenzip.