Skip to content

raise error when error value from lib is non-zero #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions tests/ziptests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ test "can compile zipfiles":

test "zipfiles extractAll":
var filename = "files/тест.xlsx"
if not fileExists(filename):
filename = "tests/files/тест.xlsx"
var z: ZipArchive
if not z.open(filename):
echo "Opening zip failed"
Expand Down Expand Up @@ -35,6 +37,11 @@ test "zipfiles read and write using Stream":

check: outStream.data == "content"

test "non-zip file raises exception":
expect IOError:
var z: ZipArchive
check (not z.open("zzzzzzzz"))

test "zipfiles read and write archive comment":
let filename = getTempDir() / "zipfiles_test_archive.zip"
defer: filename.removeFile
Expand Down
8 changes: 8 additions & 0 deletions zip/zipfiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ proc open*(z: var ZipArchive, filename: string, mode: FileMode = fmRead): bool =
z.w = zip_open(filename, flags, addr(err))
z.mode = mode
result = z.w != nil
if err != 0:
var e: ref IOError
new(e)
var buf = newString(256)
var l = zip_error_to_str(buf.cstring, 256.csize, err.cint, 0)
buf.setLen(l)
e.msg = buf
raise e

proc close*(z: var ZipArchive) =
## Closes a zip file.
Expand Down