NekoBoxForAndroid/libcore/io.go
HystericalDragon 629f99e19e
refactor(libcore): remove old APIs (#490)
Signed-off-by: HystericalDragon <HystericalDragons@proton.me>
2023-12-17 10:18:42 +08:00

79 lines
1.2 KiB
Go

package libcore
import (
"archive/zip"
"io"
"os"
"path/filepath"
"github.com/ulikunitz/xz"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
)
func Unxz(archive string, path string) error {
i, err := os.Open(archive)
if err != nil {
return err
}
r, err := xz.NewReader(i)
if err != nil {
i.Close()
return err
}
o, err := os.Create(path)
if err != nil {
i.Close()
return err
}
_, err = io.Copy(o, r)
i.Close()
return err
}
func Unzip(archive string, path string) error {
r, err := zip.OpenReader(archive)
if err != nil {
return err
}
defer r.Close()
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
for _, file := range r.File {
filePath := filepath.Join(path, file.Name)
if file.FileInfo().IsDir() {
err = os.MkdirAll(filePath, os.ModePerm)
if err != nil {
return err
}
continue
}
newFile, err := os.Create(filePath)
if err != nil {
return err
}
zipFile, err := file.Open()
if err != nil {
newFile.Close()
return err
}
var errs error
_, err = io.Copy(newFile, zipFile)
errs = E.Errors(errs, err)
errs = E.Errors(errs, common.Close(zipFile, newFile))
if errs != nil {
return errs
}
}
return nil
}