Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

.net - extract 7zip in C# code

I need use 7zip in C#. Without console, just with 7zSharp.dll ? + I find some data here

http://7zsharp.codeplex.com/releases/view/10305 ,

but I don't know how to use it( - I could create .bat(.cmd) file, but I need throught dll file) Exactly: I need extract .7z file with key)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Download the standalone console version from 7zip.com and add it to your project.

You need those 3 Files added in the project:

  1. 7za.exe
  2. 7za.dll
  3. 7zxa.dll

Don't forget to say Copy to Output Directory in it's preferences.

Extract an archive:

public void ExtractFile(string sourceArchive, string destination)
    {
        string zPath = "7za.exe"; //add to proj and set CopyToOuputDir
        try
        {
            ProcessStartInfo pro = new ProcessStartInfo();
            pro.WindowStyle = ProcessWindowStyle.Hidden;
            pro.FileName = zPath;
            pro.Arguments = string.Format("x "{0}" -y -o"{1}"", sourceArchive, destination);
            Process x = Process.Start(pro);
            x.WaitForExit();
        }
        catch (System.Exception Ex) {
            //handle error
        }
    }

Create an archive:

public void CreateZip(string sourceName, string targetArchive)
{
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = "7za.exe";
    p.Arguments = string.Format("a -tgzip "{0}" "{1}" -mx=9", targetArchive, sourceName);
    p.WindowStyle = ProcessWindowStyle.Hidden;
    Process x = Process.Start(p);
    x.WaitForExit();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...