Archived
WIP: dialog box
This commit is contained in:
1 parent
7fd6c06c78
commit
caec9c7317
109 files changed
+7397
-62
No files matched your search
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace RyzStudio.IO
|
||||
{
|
||||
public class FileType
|
||||
{
|
||||
protected static readonly byte[] BMP = { 66, 77 };
|
||||
protected static readonly byte[] GIF = { 71, 73, 70, 56 };
|
||||
protected static readonly byte[] ICO = { 0, 0, 1, 0 };
|
||||
protected static readonly byte[] JPG = { 255, 216, 255 };
|
||||
protected static readonly byte[] PNG = { 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82 };
|
||||
|
||||
public static bool IsImage(byte[] byteArray)
|
||||
{
|
||||
if (byteArray == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (byteArray.Length <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (byteArray.Take(2).SequenceEqual(BMP))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (byteArray.Take(4).SequenceEqual(GIF))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (byteArray.Take(4).SequenceEqual(ICO))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (byteArray.Take(3).SequenceEqual(JPG))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (byteArray.Take(16).SequenceEqual(PNG))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Windows.Forms;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
|
||||
namespace RyzStudio.IO
|
||||
{
|
||||
public abstract class SessionFileFormatBase
|
||||
{
|
||||
protected string CONST_KEYPASS = "";
|
||||
protected string CONST_PRODUCT = "";
|
||||
protected string CONST_STREAM_FILE_NAME = "";
|
||||
|
||||
protected bool enableErrorReporting = false;
|
||||
protected string lastUsedFileName = "";
|
||||
|
||||
#region public properties
|
||||
|
||||
public string passkey
|
||||
{
|
||||
get { return CONST_KEYPASS; }
|
||||
set { CONST_KEYPASS = value; }
|
||||
}
|
||||
|
||||
/*public string LastFilename
|
||||
{
|
||||
get { return lastUsedFileName; }
|
||||
set { lastUsedFileName = value; }
|
||||
}*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region public methods
|
||||
|
||||
/* public bool loadFromRyz()
|
||||
{
|
||||
return loadFromRyz(lastUsedFileName);
|
||||
}*/
|
||||
|
||||
public bool loadFromRyz(string file_name)
|
||||
{
|
||||
lastUsedFileName = file_name;
|
||||
if (!File.Exists(file_name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool rv = false;
|
||||
|
||||
try
|
||||
{
|
||||
ZipInputStream zipIn = new ZipInputStream(File.OpenRead(file_name));
|
||||
zipIn.Password = CONST_KEYPASS;
|
||||
ZipEntry theEntry = null;
|
||||
|
||||
while ((theEntry = zipIn.GetNextEntry()) != null)
|
||||
{
|
||||
string streamFileName = Path.GetFileName(theEntry.Name);
|
||||
if (streamFileName.Equals(CONST_STREAM_FILE_NAME))
|
||||
{
|
||||
MemoryStream oxStream = new MemoryStream();
|
||||
StreamWriter streamWriter = new StreamWriter(oxStream);
|
||||
int size = 2048;
|
||||
byte[] data = new byte[size];
|
||||
while (true)
|
||||
{
|
||||
size = zipIn.Read(data, 0, data.Length);
|
||||
if (size <= 0) break;
|
||||
streamWriter.BaseStream.Write(data, 0, size);
|
||||
}
|
||||
|
||||
oxStream.Position = 0;
|
||||
|
||||
StreamReader sr2 = new StreamReader(oxStream, Encoding.UTF8);
|
||||
XmlDocument xDoc = new XmlDocument();
|
||||
xDoc.LoadXml(sr2.ReadToEnd());
|
||||
|
||||
loadFromXmlDocument(ref xDoc);
|
||||
}
|
||||
}
|
||||
|
||||
zipIn.Close();
|
||||
|
||||
rv = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (enableErrorReporting)
|
||||
{
|
||||
MessageBox.Show(exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
public bool loadFromXml()
|
||||
{
|
||||
return loadFromXml(lastUsedFileName);
|
||||
}
|
||||
|
||||
public bool loadFromXml(string file_name)
|
||||
{
|
||||
lastUsedFileName = file_name;
|
||||
if (!File.Exists(file_name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool rv = false;
|
||||
|
||||
try
|
||||
{
|
||||
StreamReader sr2 = new StreamReader(file_name, Encoding.UTF8);
|
||||
XmlDocument xDoc = new XmlDocument();
|
||||
xDoc.LoadXml(sr2.ReadToEnd());
|
||||
|
||||
loadFromXmlDocument(ref xDoc);
|
||||
sr2.Close();
|
||||
|
||||
rv = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (enableErrorReporting)
|
||||
{
|
||||
MessageBox.Show(exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* public bool saveToRyz()
|
||||
{
|
||||
return saveToRyz(lastUsedFileName);
|
||||
}
|
||||
*/
|
||||
|
||||
public bool saveToRyz(string file_name)
|
||||
{
|
||||
bool rv = false;
|
||||
lastUsedFileName = file_name;
|
||||
byte[] buffer = new byte[4096];
|
||||
|
||||
try
|
||||
{
|
||||
File.Delete(file_name);
|
||||
FileInfo fileinfo1 = new FileInfo(file_name);
|
||||
if (!Directory.Exists(fileinfo1.DirectoryName))
|
||||
{
|
||||
Directory.CreateDirectory(fileinfo1.DirectoryName);
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (enableErrorReporting)
|
||||
{
|
||||
MessageBox.Show(exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ZipOutputStream zipOutStream1 = new ZipOutputStream(File.Create(file_name));
|
||||
zipOutStream1.SetLevel(9);
|
||||
zipOutStream1.Password = CONST_KEYPASS;
|
||||
|
||||
MemoryStream oxIndexStream = new MemoryStream();
|
||||
XmlTextWriter oxTW = new XmlTextWriter(oxIndexStream, Encoding.UTF8);
|
||||
saveToXmlTextWriter(ref oxTW);
|
||||
oxTW.Flush();
|
||||
|
||||
// write to file
|
||||
zipOutStream1.PutNextEntry(new ZipEntry(CONST_STREAM_FILE_NAME));
|
||||
oxIndexStream.Position = 0;
|
||||
StreamReader sr2 = new StreamReader(oxIndexStream, Encoding.UTF8);
|
||||
int sourceBytes;
|
||||
do
|
||||
{
|
||||
sourceBytes = sr2.BaseStream.Read(buffer, 0, buffer.Length);
|
||||
zipOutStream1.Write(buffer, 0, sourceBytes);
|
||||
}
|
||||
while (sourceBytes > 0);
|
||||
|
||||
sr2.Close();
|
||||
oxTW.Close();
|
||||
zipOutStream1.Finish();
|
||||
zipOutStream1.Close();
|
||||
|
||||
rv = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (enableErrorReporting)
|
||||
{
|
||||
MessageBox.Show(exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
public bool saveToXml()
|
||||
{
|
||||
return saveToXml(lastUsedFileName);
|
||||
}
|
||||
|
||||
public bool saveToXml(string file_name)
|
||||
{
|
||||
bool rv = false;
|
||||
|
||||
lastUsedFileName = file_name;
|
||||
try
|
||||
{
|
||||
File.Delete(file_name);
|
||||
FileInfo fileinfo1 = new FileInfo(file_name);
|
||||
if (!Directory.Exists(fileinfo1.DirectoryName))
|
||||
{
|
||||
Directory.CreateDirectory(fileinfo1.DirectoryName);
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (enableErrorReporting)
|
||||
{
|
||||
MessageBox.Show(exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
XmlTextWriter oxTW = new XmlTextWriter(file_name, Encoding.UTF8);
|
||||
saveToXmlTextWriter(ref oxTW);
|
||||
oxTW.Flush();
|
||||
oxTW.Close();
|
||||
|
||||
rv = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (enableErrorReporting)
|
||||
{
|
||||
MessageBox.Show(exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected virtual void loadFromXmlDocument(ref XmlDocument xml_doc) { }
|
||||
protected virtual void saveToXmlTextWriter(ref XmlTextWriter writer) { }
|
||||
|
||||
#region public methods (conversions)
|
||||
|
||||
public int[] convIntArrayString(string s1, char c)
|
||||
{
|
||||
string[] sarr = s1.Split(c);
|
||||
int[] iarr = new int[sarr.Length];
|
||||
for (int i = 0; i < sarr.Length; i++)
|
||||
{
|
||||
iarr[i] = Int32.Parse(sarr[i]);
|
||||
}
|
||||
|
||||
return iarr;
|
||||
}
|
||||
|
||||
public string convStringIntArray(int[] r, char s)
|
||||
{
|
||||
string t = null;
|
||||
for (int i = 0; i < r.Length; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
{
|
||||
t += s.ToString();
|
||||
}
|
||||
|
||||
t += r[i].ToString();
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
|
||||
namespace RyzStudio.IO
|
||||
{
|
||||
public class SharpZipLib
|
||||
{
|
||||
/// <summary>
|
||||
/// Is zip file encrypted
|
||||
/// </summary>
|
||||
/// <param name="fn">Filename</param>
|
||||
/// <returns>Is encrypted</returns>
|
||||
public static bool IsZipEncrypted(string fn)
|
||||
{
|
||||
bool ret_val = false;
|
||||
try
|
||||
{
|
||||
ZipInputStream zipIn = new ZipInputStream(System.IO.File.OpenRead(fn));
|
||||
ZipEntry theEntry = null;
|
||||
while ((theEntry = zipIn.GetNextEntry()) != null)
|
||||
{
|
||||
if (theEntry.IsCrypted)
|
||||
{
|
||||
ret_val = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
zipIn.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
public static void AddFile(ZipOutputStream zipstream, string filename, string prefixpath = null)
|
||||
{
|
||||
byte[] buffer = new byte[4096];
|
||||
|
||||
string f1 = "";
|
||||
if (prefixpath != null)
|
||||
{
|
||||
f1 = Path.GetDirectoryName(filename).TrimEnd('\\') + "\\";
|
||||
f1 = f1.Replace(prefixpath, "").TrimEnd('\\') + "\\";
|
||||
f1 = f1 + Path.GetFileName(filename);
|
||||
f1 = f1.TrimStart('\\');
|
||||
}
|
||||
|
||||
ZipEntry entry = new ZipEntry(f1);
|
||||
entry.DateTime = DateTime.Now;
|
||||
zipstream.PutNextEntry(entry);
|
||||
|
||||
FileStream fs = File.OpenRead(filename);
|
||||
int sourceBytes;
|
||||
do
|
||||
{
|
||||
sourceBytes = fs.Read(buffer, 0, buffer.Length);
|
||||
zipstream.Write(buffer, 0, sourceBytes);
|
||||
}
|
||||
while (sourceBytes > 0);
|
||||
}
|
||||
|
||||
public static void AddFolder(ZipOutputStream zipstream, string folderpath, string prefixpath = null)
|
||||
{
|
||||
foreach (string fn in Directory.GetFiles(folderpath, "*.*", System.IO.SearchOption.AllDirectories))
|
||||
{
|
||||
AddFile(zipstream, fn, prefixpath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user