mirror of
https://github.com/ShareX/ShareX.git
synced 2024-06-05 13:44:38 +12:00
Added ZipManager class
This commit is contained in:
parent
c33bc4844f
commit
0cc7d7525b
|
@ -25,6 +25,7 @@
|
|||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ShareX.HelpersLib
|
||||
|
@ -43,13 +44,6 @@ public SevenZipManager(string sevenZipPath)
|
|||
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 @@ public bool Extract(string archivePath, string destination, List<string> files)
|
|||
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()
|
||||
|
|
|
@ -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>
|
||||
|
|
79
ShareX.HelpersLib/ZipManager.cs
Normal file
79
ShareX.HelpersLib/ZipManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -59,9 +59,10 @@ public static bool ExtractFFmpeg(string archivePath, string extractPath)
|
|||
{
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -262,11 +262,6 @@ public static bool Export(string archivePath)
|
|||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(archivePath))
|
||||
{
|
||||
File.Delete(archivePath);
|
||||
}
|
||||
|
||||
List<string> files = new List<string>();
|
||||
|
||||
if (Settings.ExportSettings)
|
||||
|
@ -286,8 +281,9 @@ public static bool Export(string archivePath)
|
|||
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 @@ public static bool Import(string archivePath)
|
|||
{
|
||||
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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue