Uziel
Uziel

Reputation: 349

Cannot extract the contents of a TGZ file using F# and SharpZipLib

I am learning F# and Deedle. I am trying to extract the contents of this TGZ File using SharpZipLib. I downloaded the TGZ to my local drive. I think I am close because out1 works, but out2 errs. I am sure the code could be written better with pipe forwarding or composition, but it first needs to work. Does anyone have any ideas?

open ICSharpCode.SharpZipLib.GZip
open  ICSharpCode.SharpZipLib.Tar

let extract path filename =
    let fullpath = Path.Combine(path, filename)
    let inSt = File.OpenRead(fullpath)
    let gSt = new GZipInputStream(inSt)
    let tar = TarArchive.CreateOutputTarArchive(gSt)
    let out1 = tar.ListContents
    let out2 = tar.ExtractContents(path)
    out2
    
extract "C:/Downloads/" "dragontail-12.4.1.tgz"

This is the error:

Error: System.NullReferenceException: Object reference not set to an instance of an object.
   at ICSharpCode.SharpZipLib.Tar.TarArchive.ExtractContents(String destinationDirectory, Boolean allowParentTraversal) in /_/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs:line 620
   at ICSharpCode.SharpZipLib.Tar.TarArchive.ExtractContents(String destinationDirectory) in /_/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs:line 600
   at FSI_0104.extract(String path, String filename)
   at <StartupCode$FSI_0104>.$FSI_0104.main@()

Upvotes: 1

Views: 281

Answers (2)

Brian Berns
Brian Berns

Reputation: 17038

I'm not a SharpZipLib expert, but this works for me:

use tar = TarArchive.CreateInputTarArchive(gSt, System.Text.Encoding.UTF8)
tar.ExtractContents(path, true)

I think you have to explicitly allow for path traversal to unzip into an absolute path. See SharpZipLib unit tests in this file.

Upvotes: 1

Jim Foye
Jim Foye

Reputation: 2036

Does this help?

https://stackoverflow.com/a/52200001/1594263

Looks like you should be using CreateInputTarArchive(). I modified your example to use CreateInputTarArchive(), and it worked for me.

BTW you're just assigning a function to out1, you're not actually calling ListContents().

Upvotes: 2

Related Questions