Skip to content

Instantly share code, notes, and snippets.

@grishace
Created February 20, 2019 19:47
Show Gist options
  • Select an option

  • Save grishace/5feb628e447e6eed3dee282e3ece6ec7 to your computer and use it in GitHub Desktop.

Select an option

Save grishace/5feb628e447e6eed3dee282e3ece6ec7 to your computer and use it in GitHub Desktop.
Extracting 7za archive with SharpCompress without using native dependencies
open System
open System.Diagnostics
open System.IO
open FSharp.Control
open SharpCompress.Common
open SharpCompress.Readers
open SharpCompress.Archives
[<EntryPoint>]
let main argv =
let extract archive filterFn dir =
let rec asyncExtract (reader: IReader) =
asyncSeq {
if reader.MoveToNextEntry() then
if not reader.Entry.IsDirectory && filterFn(reader.Entry.Key) then
yield async {
try
use entry = reader.OpenEntryStream()
let fileName = Path.GetFileName(reader.Entry.Key)
use destination = File.OpenWrite(Path.Combine(dir, fileName))
do! entry.CopyToAsync(destination) |> Async.AwaitTask
return Ok fileName
with ex -> return Error (ex.ToString())
}
yield! asyncExtract reader
}
async {
use stream = File.OpenRead(archive)
use archive = SevenZip.SevenZipArchive.Open(stream)
use reader = archive.ExtractAllEntries()
Directory.CreateDirectory dir |> ignore
return! asyncExtract reader |> AsyncSeq.mapAsync id |> AsyncSeq.toListAsync
}
Console.ReadLine() |> ignore
let sw = Stopwatch()
sw.Start()
try
extract argv.[0] (fun f -> Path.GetExtension(f) = ".txt") @".\extracted"
|> Async.RunSynchronously |> ignore
finally
sw.Stop()
printfn "%i" sw.ElapsedMilliseconds
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment