Packer.cs 8.3 KB

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