Extracting Files From a Zip Archive With FSharp

Today I needed to unzip some archives sent to me in different formats. Since I didn't have applications like WinZip, 7Zip, or WinRar installed on my work PC I figured I could whip up a quick extractor using the built in .Net library for PDFs and a third party library for Rar files, in this case I used SharpCompress.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
open System
open System.IO
open System.IO.Compression

#r "System.IO.Compression"
#r "System.IO.Compression.FileSystem"

type Unzipper (?sourceDir : string, ?targetDir : string) =
    let mutable source =
        match sourceDir with
        | Some source1 -> source1
        | None -> Environment.CurrentDirectory
    let mutable target =
        match targetDir with
        | Some source1 -> source1
        | None -> Environment.CurrentDirectory

    let extractAll =
        Directory.EnumerateFiles(source, "*.zip")
        |> Seq.iter (fun zipPath -> printfn "Uncompressing %s" (Path.GetFileName(zipPath))
                                    let archive = ZipFile.Open(zipPath, ZipArchiveMode.Read)
                                    archive.ExtractToDirectory(target + "//" + Path.GetFileNameWithoutExtension(zipPath)))

    do extractAll

Unzipper (sourceDir = "C:/Users/powleyj/Downloads/", targetDir = "C:/Users/powleyj/Downloads/Uncompressed")

Extracting Files From a Rar Archive With FSharp

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
#r "C:/Shared Resources/SharpCompress/SharpCompress.0.24.0/lib/net45/SharpCompress.dll"

open System.IO
open SharpCompress.Common;
open SharpCompress.Readers;
open SharpCompress.Readers.Rar;

let sourceFile = "C:/Users/powleyj/Downloads/SuperImportantArchive.rar"
let destDir = "C:/Users/powleyj/Downloads/Uncompressed"

let unpackRar =
    use reader = RarReader.Open(File.OpenRead(sourceFile))
    while reader.MoveToNextEntry() do
        let extractionOptions = ExtractionOptions()
        reader.WriteEntryToDirectory(destDir, extractionOptions)

unpackRar
namespace System
namespace System.IO
namespace System.IO.Compression
Multiple items
type Unzipper =
  new : ?sourceDir:string * ?targetDir:string -> Unzipper

Full name: extractingfilesfromzipandrararchives.Unzipper

--------------------
new : ?sourceDir:string * ?targetDir:string -> Unzipper
val sourceDir : string option
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val targetDir : string option
val mutable source : string
union case Option.Some: Value: 'T -> Option<'T>
val source1 : string
union case Option.None: Option<'T>
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
property Environment.CurrentDirectory: string
val mutable target : string
val extractAll : unit
type Directory =
  static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
  static member Delete : path:string -> unit + 1 overload
  static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
  static member Exists : path:string -> bool
  static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
  static member GetCreationTime : path:string -> DateTime
  static member GetCreationTimeUtc : path:string -> DateTime
  static member GetCurrentDirectory : unit -> string
  ...

Full name: System.IO.Directory
Directory.EnumerateFiles(path: string) : Collections.Generic.IEnumerable<string>
Directory.EnumerateFiles(path: string, searchPattern: string) : Collections.Generic.IEnumerable<string>
Directory.EnumerateFiles(path: string, searchPattern: string, searchOption: SearchOption) : Collections.Generic.IEnumerable<string>
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val zipPath : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
Path.GetFileName(path: string) : string
val archive : ZipArchive
type ZipFile =
  static member CreateFromDirectory : sourceDirectoryName:string * destinationArchiveFileName:string -> unit + 2 overloads
  static member ExtractToDirectory : sourceArchiveFileName:string * destinationDirectoryName:string -> unit + 1 overload
  static member Open : archiveFileName:string * mode:ZipArchiveMode -> ZipArchive + 1 overload
  static member OpenRead : archiveFileName:string -> ZipArchive

Full name: System.IO.Compression.ZipFile
ZipFile.Open(archiveFileName: string, mode: ZipArchiveMode) : ZipArchive
ZipFile.Open(archiveFileName: string, mode: ZipArchiveMode, entryNameEncoding: Text.Encoding) : ZipArchive
type ZipArchiveMode =
  | Read = 0
  | Create = 1
  | Update = 2

Full name: System.IO.Compression.ZipArchiveMode
field ZipArchiveMode.Read = 0
(extension) ZipArchive.ExtractToDirectory(destinationDirectoryName: string) : unit
Path.GetFileNameWithoutExtension(path: string) : string
namespace SharpCompress
namespace SharpCompress.Common
namespace SharpCompress.Readers
namespace SharpCompress.Readers.Rar
val sourceFile : string

Full name: extractingfilesfromzipandrararchives.sourceFile
val destDir : string

Full name: extractingfilesfromzipandrararchives.destDir
val unpackRar : unit

Full name: extractingfilesfromzipandrararchives.unpackRar
val reader : RarReader
type RarReader =
  inherit AbstractReader<RarReaderEntry, RarVolume>
  member Volume : RarVolume
  static member Open : stream:Stream * ?options:ReaderOptions -> RarReader + 1 overload

Full name: SharpCompress.Readers.Rar.RarReader
RarReader.Open(streams: Collections.Generic.IEnumerable<Stream>, ?options: ReaderOptions) : RarReader
RarReader.Open(stream: Stream, ?options: ReaderOptions) : RarReader
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.OpenRead(path: string) : FileStream
AbstractReader.MoveToNextEntry() : bool
val extractionOptions : ExtractionOptions
Multiple items
type ExtractionOptions =
  new : unit -> ExtractionOptions
  val WriteSymbolicLink : SymbolicLinkWriterDelegate
  member ExtractFullPath : bool with get, set
  member Overwrite : bool with get, set
  member PreserveAttributes : bool with get, set
  member PreserveFileTime : bool with get, set
  nested type SymbolicLinkWriterDelegate

Full name: SharpCompress.Common.ExtractionOptions

--------------------
ExtractionOptions() : unit
(extension) IReader.WriteEntryToDirectory(destinationDirectory: string, ?options: ExtractionOptions) : unit