1

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@()
Uziel
  • 349
  • 4
  • 9

2 Answers2

2

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().

Jim Foye
  • 1,918
  • 1
  • 13
  • 15
1

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.

Brian Berns
  • 15,499
  • 2
  • 30
  • 40
  • 'System.Text.Encoding.UTF8' is the piece that made the difference. I have no idea how I would have known to use that, but thank you! – Uziel Feb 22 '22 at 17:53