[commit: Cabal] master: Added GZipUtils to handle .tar files with the same code as .tar.gz (d2e60eb)
Ian Lynagh
igloo at earth.li
Fri Jun 24 02:06:52 CEST 2011
Repository : ssh://darcs.haskell.org//srv/darcs/packages/Cabal
On branch : master
http://hackage.haskell.org/trac/ghc/changeset/d2e60eb23827b188a3a558db247d0096825f0f66
>---------------------------------------------------------------
commit d2e60eb23827b188a3a558db247d0096825f0f66
Author: Dmitry Astapov <dastapov at gmail.com>
Date: Tue Oct 26 20:23:43 2010 +0000
Added GZipUtils to handle .tar files with the same code as .tar.gz
>---------------------------------------------------------------
cabal-install/Distribution/Client/GZipUtils.hs | 38 ++++++++++++++++++++++++
cabal-install/cabal-install.cabal | 1 +
2 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/cabal-install/Distribution/Client/GZipUtils.hs b/cabal-install/Distribution/Client/GZipUtils.hs
new file mode 100644
index 0000000..c969c11
--- /dev/null
+++ b/cabal-install/Distribution/Client/GZipUtils.hs
@@ -0,0 +1,38 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Distribution.Client.GZipUtils
+-- Copyright : (c) Dmitry Astapov 2010
+-- License : BSD-like
+--
+-- Maintainer : cabal-devel at gmail.com
+-- Stability : provisional
+-- Portability : portable
+--
+-- Provides a convenience functions for working with files that may or may not
+-- be zipped.
+-----------------------------------------------------------------------------
+module Distribution.Client.GZipUtils (
+ maybeDecompress,
+ ) where
+
+import qualified Data.ByteString.Lazy.Internal as BS (ByteString(..))
+import Data.ByteString.Lazy (ByteString)
+import Codec.Compression.GZip
+import Codec.Compression.Zlib.Internal
+
+-- | Attempts to decompress the `bytes' under the assumption that
+-- "data format" error at the very beginning of the stream means
+-- that it is already decompressed. Caller should make sanity checks
+-- to verify that it is not, in fact, garbage.
+--
+maybeDecompress :: ByteString -> ByteString
+maybeDecompress bytes = foldStream $ decompressWithErrors gzipOrZlibFormat defaultDecompressParams bytes
+ where
+ -- DataError at the beginning of the stream probably means that stream is not compressed.
+ -- Returning it as-is.
+ foldStream (StreamError DataError _) = bytes
+ foldStream somethingElse = doFold somethingElse
+
+ doFold StreamEnd = BS.Empty
+ doFold (StreamChunk bs stream) = BS.Chunk bs (doFold stream)
+ doFold (StreamError _ msg) = error $ "Codec.Compression.Zlib: " ++ msg
diff --git a/cabal-install/cabal-install.cabal b/cabal-install/cabal-install.cabal
index e085378..e07b372 100644
--- a/cabal-install/cabal-install.cabal
+++ b/cabal-install/cabal-install.cabal
@@ -56,6 +56,7 @@ Executable cabal
Distribution.Client.Dependency.TopDown.Types
Distribution.Client.Dependency.Types
Distribution.Client.Fetch
+ Distribution.Client.GZipUtils
Distribution.Client.Haddock
Distribution.Client.HttpUtils
Distribution.Client.IndexUtils
More information about the Cvs-libraries
mailing list