Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Last active February 14, 2016 03:36
Show Gist options
  • Select an option

  • Save AlexeyRaga/3e4be1fceb5860b4277b to your computer and use it in GitHub Desktop.

Select an option

Save AlexeyRaga/3e4be1fceb5860b4277b to your computer and use it in GitHub Desktop.
Streaming ungzip with error handling
name: andy
version: 0.1.0.0
synopsis: Simple project template from stack
description: Please see README.md
license: BSD3
license-file: LICENSE
author: Alexey Raga
maintainer: alexey.raga@somewhere.else
copyright: Alexey Raga
category: Services
build-type: Simple
cabal-version: >=1.10
executable andy
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, bytestring
, directory
, filemanip
, filepath
, mtl
, regex-posix
, unix
, unix-compat
, xml
, zlib
, conduit
, bzlib-conduit
, binary-conduit
, conduit-extra
, resourcet
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Exception
import Control.Monad.Trans.Resource (runResourceT)
import Data.ByteString (ByteString)
import Data.Conduit (($$), ($=), catchC, yield)
import Data.Conduit.Binary
import Data.Conduit.Zlib
import System.IO
bad = "/Users/alexey/Work/run.sh"
good = "/Users/alexey/Work/split.py.gz"
main = unzipFile bad
unzipFile :: String -> IO ()
unzipFile name = runResourceT $
sourceFile name
$= (ungzip `catchC` (yield . compensate))
$$ sinkHandle stdout
compensate :: SomeException -> ByteString
compensate _ = "No data for you bro"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment