Packer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // CoreUtil
  2. //
  3. // Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
  4. // Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
  5. // Comments: Tetsuo Sugiyama, Ph.D.
  6. //
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License
  10. // version 2 as published by the Free Software Foundation.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License version 2
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  25. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  27. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  30. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  31. //
  32. //
  33. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  34. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  35. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  36. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  37. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  38. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  39. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  40. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  41. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  42. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  43. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  44. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  45. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  46. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  47. //
  48. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  49. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  50. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  51. // COUNTRIES MIGHT BE RESTRICTED.
  52. //
  53. //
  54. // SOURCE CODE CONTRIBUTION
  55. // ------------------------
  56. //
  57. // Your contribution to SoftEther VPN Project is much appreciated.
  58. // Please send patches to us through GitHub.
  59. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  60. // http://www.softether.org/5-download/src/9.patch
  61. //
  62. //
  63. // DEAR SECURITY EXPERTS
  64. // ---------------------
  65. //
  66. // If you find a bug or a security vulnerability please kindly inform us
  67. // about the problem immediately so that we can fix the security problem
  68. // to protect a lot of users around the world as soon as possible.
  69. //
  70. // Our e-mail address for security reports is:
  71. // softether-vpn-security [at] softether.org
  72. //
  73. // Please note that the above e-mail address is not a technical support
  74. // inquiry address. If you need technical assistance, please visit
  75. // http://www.softether.org/ and ask your question on the users forum.
  76. //
  77. // Thank you for your cooperation.
  78. using System;
  79. using System.Threading;
  80. using System.Data;
  81. using System.Data.Sql;
  82. using System.Data.SqlClient;
  83. using System.Data.SqlTypes;
  84. using System.Text;
  85. using System.Configuration;
  86. using System.Collections;
  87. using System.Collections.Generic;
  88. using System.Security.Cryptography;
  89. using System.Web;
  90. using System.Web.Security;
  91. using System.Web.UI;
  92. using System.Web.UI.WebControls;
  93. using System.Web.UI.WebControls.WebParts;
  94. using System.Web.UI.HtmlControls;
  95. using System.IO;
  96. using System.Drawing;
  97. using System.Drawing.Imaging;
  98. using System.Drawing.Drawing2D;
  99. using System.Runtime.InteropServices;
  100. namespace CoreUtil
  101. {
  102. public enum PackerFileFormat
  103. {
  104. ZipRaw,
  105. ZipCompressed,
  106. Tar,
  107. TarGZip,
  108. }
  109. public delegate bool ProgressDelegate(string fileNameFullPath, string fileNameRelative, int currentFileNum, int totalFileNum);
  110. public static class Packer
  111. {
  112. public static byte[] PackDir(PackerFileFormat format, string rootDirPath, string appendPrefixDirName)
  113. {
  114. return PackDir(format, rootDirPath, appendPrefixDirName, null);
  115. }
  116. public static byte[] PackDir(PackerFileFormat format, string topDirPath, string appendPrefixDirName, ProgressDelegate proc)
  117. {
  118. string[] fileList = Directory.GetFiles(topDirPath, "*", SearchOption.AllDirectories);
  119. List<string> relativeFileList = new List<string>();
  120. foreach (string fileName in fileList)
  121. {
  122. string relativePath = IO.GetRelativeFileName(fileName, topDirPath);
  123. if (Str.IsEmptyStr(appendPrefixDirName) == false)
  124. {
  125. relativePath = IO.RemoteLastEnMark(appendPrefixDirName) + "\\" + relativePath;
  126. }
  127. relativeFileList.Add(relativePath);
  128. }
  129. return PackFiles(format, fileList, relativeFileList.ToArray(), proc);
  130. }
  131. public static byte[] PackFiles(PackerFileFormat format, string[] srcFileNameList, string[] relativeNameList)
  132. {
  133. return PackFiles(format, srcFileNameList, relativeNameList, null);
  134. }
  135. public static byte[] PackFiles(PackerFileFormat format, string[] srcFileNameList, string[] relativeNameList, ProgressDelegate proc)
  136. {
  137. if (srcFileNameList.Length != relativeNameList.Length)
  138. {
  139. throw new ApplicationException("srcFileNameList.Length != relativeNameList.Length");
  140. }
  141. int num = srcFileNameList.Length;
  142. int i;
  143. ZipPacker zip = new ZipPacker();
  144. TarPacker tar = new TarPacker();
  145. for (i = 0; i < num; i++)
  146. {
  147. if (proc != null)
  148. {
  149. bool ret = proc(srcFileNameList[i], relativeNameList[i], i, num);
  150. if (ret == false)
  151. {
  152. continue;
  153. }
  154. }
  155. byte[] srcData = File.ReadAllBytes(srcFileNameList[i]);
  156. DateTime date = File.GetLastWriteTime(srcFileNameList[i]);
  157. switch (format)
  158. {
  159. case PackerFileFormat.Tar:
  160. case PackerFileFormat.TarGZip:
  161. tar.AddFileSimple(relativeNameList[i], srcData, 0, srcData.Length, date);
  162. break;
  163. case PackerFileFormat.ZipRaw:
  164. case PackerFileFormat.ZipCompressed:
  165. zip.AddFileSimple(relativeNameList[i], date, FileAttributes.Normal, srcData, (format == PackerFileFormat.ZipCompressed));
  166. break;
  167. }
  168. }
  169. switch (format)
  170. {
  171. case PackerFileFormat.Tar:
  172. tar.Finish();
  173. return tar.GeneratedData.Read();
  174. case PackerFileFormat.TarGZip:
  175. tar.Finish();
  176. return tar.CompressToGZip();
  177. case PackerFileFormat.ZipCompressed:
  178. case PackerFileFormat.ZipRaw:
  179. zip.Finish();
  180. return zip.GeneratedData.Read();
  181. default:
  182. throw new ApplicationException("format");
  183. }
  184. }
  185. }
  186. }
  187. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  188. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  189. // Join us: http://www.tsukuba.ac.jp/english/admission/