2019-04-21 17:53:56 +00:00
|
|
|
|
namespace RyzStudio.IO
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
|
|
|
|
2017-07-30 11:59:34 +00:00
|
|
|
|
public class SharpZipLib
|
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
public static bool IsZipEncrypted(string filename)
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
bool rv = false;
|
|
|
|
|
|
2017-07-30 11:59:34 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
ZipInputStream zipStream = new ZipInputStream(File.OpenRead(filename));
|
|
|
|
|
|
|
|
|
|
ZipEntry zipEntry = null;
|
|
|
|
|
while ((zipEntry = zipStream.GetNextEntry()) != null)
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
if (zipEntry.IsCrypted)
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
rv = true;
|
2017-07-30 11:59:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 17:53:56 +00:00
|
|
|
|
zipStream.Close();
|
2017-07-30 11:59:34 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
2019-04-21 17:53:56 +00:00
|
|
|
|
{
|
2017-07-30 11:59:34 +00:00
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 17:53:56 +00:00
|
|
|
|
return rv;
|
2017-07-30 11:59:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 17:53:56 +00:00
|
|
|
|
public static bool TestZipEncrypted(string filename, string password)
|
|
|
|
|
{
|
|
|
|
|
bool rv = false;
|
|
|
|
|
|
|
|
|
|
int size = 2;
|
|
|
|
|
byte[] buffer = new byte[size];
|
|
|
|
|
int bufferSize = 0;
|
|
|
|
|
|
|
|
|
|
ZipEntry readEntry = null;
|
|
|
|
|
ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename));
|
|
|
|
|
readStream.Password = password;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
readEntry = readStream.GetNextEntry();
|
|
|
|
|
if (readEntry == null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(readEntry.Name))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!readEntry.IsFile)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (readEntry.Size <= 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bufferSize = readStream.Read(buffer, 0, buffer.Length);
|
|
|
|
|
rv = true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AddFile(ZipOutputStream zipStream, string filename, string prefixPath = null)
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[4096];
|
2019-04-21 17:53:56 +00:00
|
|
|
|
|
2017-07-30 11:59:34 +00:00
|
|
|
|
string f1 = "";
|
2019-04-21 17:53:56 +00:00
|
|
|
|
if (prefixPath != null)
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
|
|
|
|
f1 = Path.GetDirectoryName(filename).TrimEnd('\\') + "\\";
|
2019-04-21 17:53:56 +00:00
|
|
|
|
f1 = f1.Replace(prefixPath, "").TrimEnd('\\') + "\\";
|
2017-07-30 11:59:34 +00:00
|
|
|
|
f1 = f1 + Path.GetFileName(filename);
|
|
|
|
|
f1 = f1.TrimStart('\\');
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 17:53:56 +00:00
|
|
|
|
ZipEntry zipEntry = new ZipEntry(f1);
|
|
|
|
|
zipEntry.DateTime = DateTime.Now;
|
|
|
|
|
zipStream.PutNextEntry(zipEntry);
|
|
|
|
|
|
|
|
|
|
FileStream fileStream = File.OpenRead(filename);
|
2017-07-30 11:59:34 +00:00
|
|
|
|
int sourceBytes;
|
|
|
|
|
do
|
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
|
|
|
|
|
zipStream.Write(buffer, 0, sourceBytes);
|
2017-07-30 11:59:34 +00:00
|
|
|
|
}
|
|
|
|
|
while (sourceBytes > 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 17:53:56 +00:00
|
|
|
|
public static void AddFolder(ZipOutputStream zipStream, string folderPath, string prefixPath = null)
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
foreach (string fn in Directory.GetFiles(folderPath, "*.*", System.IO.SearchOption.AllDirectories))
|
2017-07-30 11:59:34 +00:00
|
|
|
|
{
|
2019-04-21 17:53:56 +00:00
|
|
|
|
AddFile(zipStream, fn, prefixPath);
|
2017-07-30 11:59:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|