Code Monkey home page Code Monkey logo

archivemanagersharp's People

Watchers

 avatar  avatar  avatar

Forkers

emilnachev

archivemanagersharp's Issues

RAR files extract corrupted files if the filenames are not encrypted

When a file from the viewing window of the program is opened or extracted from a password-protected rar file, the resulting file is corrupted. The problem is the library this application uses to open the archive, SharpCompress, does not have a check for passwords for rars and also does not have validation for file data, like CRC. I may have to find a way to validate the file, possibly by the file size because it seems to create an empty file.

Need to keep an eye on CRC Validation support in SharpCompress: adamhathcock/sharpcompress#246

Demo

public static class UniversalArchiver
{
    public static void ExtractAll(string archiveFilePath, string destinationFolder, string password)
    {
        Directory.CreateDirectory(destinationFolder);

        var fileInfo = new FileInfo(archiveFilePath);
        var readerOptions = new ReaderOptions { Password = password };
        var extractionOptions = new ExtractionOptions { ExtractFullPath = true, Overwrite = true, PreserveAttributes = false, PreserveFileTime = true };
        var archiveType = GetArchiveType(Path.GetExtension(archiveFilePath));

        IArchive archive;
        switch (archiveType)
        {
            case ArchiveType.Rar:
                archive = RarArchive.Open(fileInfo, readerOptions);
                break;
            case ArchiveType.Zip:
                archive = ZipArchive.Open(fileInfo, readerOptions);
                break;
            case ArchiveType.Tar:
                archive = TarArchive.Open(fileInfo, readerOptions);
                break;
            case ArchiveType.SevenZip:
                archive = SevenZipArchive.Open(fileInfo, readerOptions);
                break;
            case ArchiveType.GZip:
                archive = GZipArchive.Open(fileInfo, readerOptions);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(archiveType), archiveType, null);
        }

        archive.WriteToDirectory(destinationFolder, extractionOptions);
        archive.Dispose();
    }

    private static ArchiveType GetArchiveType(string extension)
    {
        switch (extension.Replace(".", string.Empty).ToLowerInvariant())
        {
            case "rar":
                return ArchiveType.Rar;
            case "zip":
            case "zipx":
                return ArchiveType.Zip;
            case "tar":
                return ArchiveType.Tar;
            case "7z":
                return ArchiveType.SevenZip;
            case "gz":
                return ArchiveType.GZip;
            default:
                throw new ArchiveException("Unknown file type!");
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.