Added ZipManager class

This commit is contained in:
Jaex 2018-03-02 23:46:39 +03:00
parent c33bc4844f
commit 0cc7d7525b
5 changed files with 104 additions and 18 deletions

View File

@ -25,6 +25,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace ShareX.HelpersLib
@ -43,13 +44,6 @@ namespace ShareX.HelpersLib
SevenZipPath = sevenZipPath;
}
public bool Compress(string archivePath, List<string> files, string workingDirectory = "")
{
string fileArgs = string.Join(" ", files.Select(x => $"\"{x}\""));
string arguments = $"a -tzip \"{archivePath}\" {fileArgs} -mx=9";
return Run(arguments, workingDirectory) == 0;
}
public bool Extract(string archivePath, string destination)
{
string arguments = $"x \"{archivePath}\" -o\"{destination}\" -y";
@ -63,6 +57,18 @@ namespace ShareX.HelpersLib
return Run(arguments) == 0;
}
public bool Compress(string archivePath, List<string> files, string workingDirectory = "")
{
if (File.Exists(archivePath))
{
File.Delete(archivePath);
}
string fileArgs = string.Join(" ", files.Select(x => $"\"{x}\""));
string arguments = $"a -tzip \"{archivePath}\" {fileArgs} -mx=9";
return Run(arguments, workingDirectory) == 0;
}
private int Run(string arguments, string workingDirectory = "")
{
ProcessStartInfo startInfo = new ProcessStartInfo()

View File

@ -90,6 +90,8 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Web" />
@ -466,6 +468,7 @@
<Compile Include="WritablePropertiesOnlyResolver.cs" />
<Compile Include="XmlColor.cs" />
<Compile Include="XmlFont.cs" />
<Compile Include="ZipManager.cs" />
<EmbeddedResource Include="Colors\ColorPickerForm.de.resx">
<DependentUpon>ColorPickerForm.cs</DependentUpon>
</EmbeddedResource>

View File

@ -0,0 +1,79 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2018 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShareX.HelpersLib
{
public class ZipManager
{
public void Extract(string archivePath, string destination)
{
ZipFile.ExtractToDirectory(archivePath, destination);
}
public void Extract(string archivePath, string destination, List<string> files)
{
using (ZipArchive archive = ZipFile.OpenRead(archivePath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string entryName = entry.Name;
foreach (string file in files)
{
if (file.Equals(entryName, StringComparison.InvariantCultureIgnoreCase))
{
entry.ExtractToFile(Path.Combine(destination, entryName));
break;
}
}
}
}
}
public void Compress(string archivePath, List<string> files, string workingDirectory = "")
{
if (File.Exists(archivePath))
{
File.Delete(archivePath);
}
using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Update))
{
foreach (string file in files)
{
archive.CreateEntryFromFile(Path.Combine(workingDirectory, file), file);
}
}
}
}
}

View File

@ -59,9 +59,10 @@ namespace ShareX.MediaLib
{
try
{
SevenZipManager sevenZipManager = new SevenZipManager();
ZipManager zipManager = new ZipManager();
List<string> files = new List<string>() { "ffmpeg.exe" };
return sevenZipManager.Extract(archivePath, extractPath, files);
zipManager.Extract(archivePath, extractPath, files);
return true;
}
catch (Exception e)
{

View File

@ -262,11 +262,6 @@ namespace ShareX
{
try
{
if (File.Exists(archivePath))
{
File.Delete(archivePath);
}
List<string> files = new List<string>();
if (Settings.ExportSettings)
@ -286,8 +281,9 @@ namespace ShareX
files.Add($"{Program.LogsFoldername}\\*.txt");
}
SevenZipManager sevenZipManager = new SevenZipManager();
return sevenZipManager.Compress(archivePath, files, Program.PersonalFolder);
ZipManager zipManager = new ZipManager();
zipManager.Compress(archivePath, files, Program.PersonalFolder);
return true;
}
catch (Exception e)
{
@ -302,8 +298,9 @@ namespace ShareX
{
try
{
SevenZipManager sevenZipManager = new SevenZipManager();
return sevenZipManager.Extract(archivePath, Program.PersonalFolder);
ZipManager zipManager = new ZipManager();
zipManager.Extract(archivePath, Program.PersonalFolder);
return true;
}
catch (Exception e)
{