Skip to content

Instantly share code, notes, and snippets.

@danielecook
Created April 12, 2019 11:38
Show Gist options
  • Save danielecook/96c383ea82a2515fd63c91add9af96c4 to your computer and use it in GitHub Desktop.
Save danielecook/96c383ea82a2515fd63c91add9af96c4 to your computer and use it in GitHub Desktop.
Read gzipped file in nim
import
zlib, streams
type
GZipStream* = object of StreamObj
f: GzFile
GzipStreamRef* = ref GZipStream
proc fsClose(s: Stream) =
discard gzclose(GZipStreamRef(s).f)
proc fsReadData(s: Stream, buffer: pointer, bufLen: int): int =
return gzread(GZipStreamRef(s).f, buffer, bufLen)
proc fsAtEnd(s: Stream): bool =
return gzeof(GZipStreamRef(s).f) != 0
proc newGZipStream*(f: GzFile): GZipStreamRef =
new result
result.f = f
result.closeImpl = fsClose
result.readDataImpl = fsReadData
result.atEndImpl = fsAtEnd
# other methods are nil!
proc newGZipStream*(filename: cstring): GZipStreamRef =
var gz = gzopen(filename, "r")
if gz != nil: return newGZipStream(gz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment