|
using System;
using System.IO;
using System.Text;
namespace HttpZipFolder
{
static class ZipUtils
{
internal const uint LocalFileHeaderSignature = 0x04034b50;
internal const uint DataDescriptorSignature = 0x08074b50;
internal const uint FileHeaderSignature = 0x02014b50;
internal const uint EndOfCentralDirectoryRecordSignature = 0x06054b50;
internal const ushort NoCompressionMethod = 0;
internal const ushort DeflateCompressionMethod = 8;
internal static uint Read32(Stream s)
{
return BitConverter.ToUInt32(ReadBytes(s, 4), 0);
}
internal static ushort Read16(Stream s)
{
return BitConverter.ToUInt16(ReadBytes(s, 2), 0);
}
internal static byte[] ReadBytes(Stream s, int length)
{
byte[] buffer = new byte[length];
int read = s.Read(buffer, 0, length);
if (read != length) return null;
return buffer;
}
internal static void Set16(byte[] buffer, int offset, ushort value)
{
byte[] data = BitConverter.GetBytes(value);
Array.Copy(data, 0, buffer, offset, data.Length);
}
internal static void Set32(byte[] buffer, int offset, uint value)
{
byte[] data = BitConverter.GetBytes(value);
Array.Copy(data, 0, buffer, offset, data.Length);
}
internal static ushort Get16(byte[] buffer, int offset)
{
return BitConverter.ToUInt16(buffer, offset);
}
internal static uint Get32(byte[] buffer, int offset)
{
return BitConverter.ToUInt32(buffer, offset);
}
internal static void Write32(Stream s, uint value)
{
byte[] data = BitConverter.GetBytes(value);
s.Write(data, 0, data.Length);
}
internal static string ConvertToString(byte[] data)
{
return Encoding.Default.GetString(data);
}
internal static byte[] ConvertFromString(string s)
{
return Encoding.Default.GetBytes(s);
}
internal static DateTime ConvertToDataTime(uint packedDateTime)
{
int year = 1980 + (int)((packedDateTime >> 25) & 0x7F);
int month = (int)(packedDateTime >> 21) & 0x0F;
int day = (int)(packedDateTime >> 16) & 0x1F;
int hours = (int)(packedDateTime >> 11) & 0x1F;
int minutes = (int)(packedDateTime >> 5) & 0x3F;
int seconds = (int)(packedDateTime & 0x1F) << 1;
return new DateTime(year, month, day, hours, minutes, seconds);
}
internal static uint ConvertFromDataTime(DateTime dateTime)
{
int withoutYear = dateTime.Month << 21 | dateTime.Day << 16 |
dateTime.Hour << 11 | dateTime.Minute << 5 | (dateTime.Second >> 1);
return (uint)withoutYear | (uint)((dateTime.Year - 1980) << 25);
}
internal class LocalFileHeaderOffset
{
internal const int Version = 0;
internal const int Flag = 2;
internal const int CompressionMethod = 4;
internal const int Modified = 6;
internal const int Crc32 = 10;
internal const int CompressedSize = 14;
internal const int Size = 18;
internal const int FilenameLength = 22;
internal const int ExtrasLength = 24;
internal const int TotalStructureSize = 26;
}
internal class FileHeaderOffset
{
internal const int VersionMade = 0;
internal const int Version = 2;
internal const int Flag = 4;
internal const int CompressionMethod = 6;
internal const int Modified = 8;
internal const int Crc32 = 12;
internal const int CompressedSize = 16;
internal const int Size = 20;
internal const int FilenameLength = 24;
internal const int ExtrasLength = 26;
internal const int CommentsLength = 28;
internal const int TotalStructureSize = 42;
}
internal class EndOfCentralDirectoryOffset
{
internal const int Disk = 0;
internal const int StartDisk = 2;
internal const int DiskEntries = 4;
internal const int TotalEntries = 6;
internal const int DirectorySize = 8;
internal const int DirectoryOffset = 12;
internal const int CommentsLength = 16;
internal const int TotalStructureSize = 18;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.