Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
// IOFunctions.cs created with MonoDevelop
// User: alan at 14:43 20/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Runtime.InteropServices;
namespace zipsharp
{
// this matches a native 'enum', don't modify
enum Append
{
Create = 0,
CreateAfter = 1,
AddInZip = 2
}
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate IntPtr OpenFileFunc (IntPtr opaque, string filename, int mode);
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate /* ulong */ IntPtr ReadFileFunc (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ IntPtr size);
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate /* ulong */ IntPtr WriteFileFunc (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ IntPtr size);
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate /* long */ IntPtr TellFileFunc (IntPtr opaque, IntPtr stream);
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate /* long */ IntPtr SeekFileFunc (IntPtr opaque, IntPtr stream, /* ulong */ IntPtr offset, int origin);
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate int CloseFileFunc (IntPtr opaque, IntPtr stream);
[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
internal delegate int TestErrorFileFunc (IntPtr opaque, IntPtr stream);
[StructLayout (LayoutKind.Sequential)]
internal struct ZlibFileFuncDef
{
[MarshalAs (UnmanagedType.FunctionPtr)] public OpenFileFunc zopen_file;
[MarshalAs (UnmanagedType.FunctionPtr)] public ReadFileFunc zread_file;
[MarshalAs (UnmanagedType.FunctionPtr)] public WriteFileFunc zwrite_file;
[MarshalAs (UnmanagedType.FunctionPtr)] public TellFileFunc ztell_file;
[MarshalAs (UnmanagedType.FunctionPtr)] public SeekFileFunc zseek_file;
[MarshalAs (UnmanagedType.FunctionPtr)] public CloseFileFunc zclose_file;
[MarshalAs (UnmanagedType.FunctionPtr)] public TestErrorFileFunc zerror_file;
public IntPtr opaque;
}
}

View File

@@ -0,0 +1,198 @@
// NativeUnzip.cs created with MonoDevelop
// User: alan at 13:11 20/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Runtime.InteropServices;
using System.Text;
namespace zipsharp
{
static class NativeUnzip
{
enum ZipStringComparison
{
OSDefault = 0,
CaseSensitive = 1,
CaseInsensitive = 2
}
public static void CloseArchive (UnzipHandle handle)
{
unzClose (handle);
handle.SetHandleAsInvalid ();
}
public static void CloseCurrentFile (UnzipHandle handle)
{
if (unzCloseCurrentFile (handle) != 0)
throw new Exception ("Could not close the active file");
}
static CompressionOption ConvertCompression (int compressionLevel)
{
switch (compressionLevel)
{
case 1:
case 2:
return CompressionOption.SuperFast;
case 3:
case 4:
return CompressionOption.Fast;
case 5:
case 6:
return CompressionOption.Normal;
case 7:
case 8:
case 9:
return CompressionOption.Maximum;
default:
return CompressionOption.NotCompressed;
}
}
public static long CurrentFilePosition (UnzipHandle handle)
{
return unztell(handle).ToInt64 ();
}
public static long CurrentFileLength (UnzipHandle handle)
{
UnzipFileInfo info;
StringBuilder sbName = new StringBuilder (128);
int result = unzGetCurrentFileInfo (handle, out info, sbName, new IntPtr (sbName.Capacity), IntPtr.Zero, IntPtr.Zero, null, IntPtr.Zero);
if (result != 0)
return -1;
else
return (long)info.UncompressedSize;
}
static string GetCurrentFileName (UnzipHandle handle)
{
UnzipFileInfo info;
StringBuilder sbName = new StringBuilder (128);
int result = unzGetCurrentFileInfo (handle, out info, sbName, new IntPtr (sbName.Capacity), IntPtr.Zero, new IntPtr (0), null, IntPtr.Zero);
if (result != 0)
return null;
else
return sbName.ToString ();
}
public static string[] GetFiles (UnzipHandle handle)
{
List<string> files = new List<string> ();
GoToFirstFile (handle);
string name;
while ((name = GetCurrentFileName(handle)) != null)
{
files.Add (name);
if (!NativeUnzip.GoToNextFile (handle))
break;
}
return files.ToArray ();
}
static void GoToFirstFile (UnzipHandle handle)
{
if (NativeUnzip.unzGoToFirstFile (handle) != 0)
throw new Exception ("Zip file is invalid");
}
static bool GoToNextFile (UnzipHandle handle)
{
return unzGoToNextFile(handle) == 0;
}
public static UnzipHandle OpenArchive (ZlibFileFuncDef fileFuncs)
{
UnzipHandle handle = unzOpen2 ("", ref fileFuncs);
if (handle.IsInvalid)
throw new Exception ("Could not open unzip archive");
return handle;
}
public static void OpenFile (UnzipHandle handle, string name, out CompressionOption level)
{
if (unzLocateFile (handle, name, (int) ZipStringComparison.CaseInsensitive) != 0)
throw new Exception ("The file doesn't exist");
int method, compression;
// '0' means do not open in raw mode (raw == do not decompress)
if (unzOpenCurrentFile2 (handle, out method, out compression, 0) != 0)
throw new Exception ("The file could not be opened");
level = ConvertCompression (method == 0 ? 0 : compression);
}
public static unsafe int Read (UnzipHandle handle, byte[] buffer, int offset, int count)
{
if ((buffer.Length - offset) > count)
throw new ArgumentOutOfRangeException ("count", "Buffer is too small to read that amount of data");
fixed (byte * b = &buffer[offset])
return unzReadCurrentFile (handle, b, (uint)count);
}
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzCloseCurrentFile (UnzipHandle handle);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern IntPtr unztell (UnzipHandle handle);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzGoToFirstFile (UnzipHandle handle);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern UnzipHandle unzOpen2 (string path,
ref ZlibFileFuncDef pzlib_filefunc_def);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzGoToNextFile (UnzipHandle handle);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzLocateFile (UnzipHandle handle,
string szFileName,
int iCaseSensitivity);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzOpenCurrentFile2 (UnzipHandle handle,
out int method,
out int level,
int raw);
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzGetCurrentFileInfo (UnzipHandle handle,
out UnzipFileInfo pfile_info,
StringBuilder szFileName,
IntPtr fileNameBufferSize, // uLong
IntPtr extraField, // void *
IntPtr extraFieldBufferSize, // uLong
StringBuilder szComment,
IntPtr commentBufferSize); // uLong
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static unsafe extern int unzReadCurrentFile (UnzipHandle handle,
byte* buf, // voidp
uint len);
//[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
//static extern int unzSetOffset (UnzipHandle handle, IntPtr pos); // uLong
[DllImport ("MonoPosixHelper", CallingConvention=CallingConvention.Cdecl)]
static extern int unzClose (UnzipHandle handle);
}
}

View File

@@ -0,0 +1,90 @@
// Native.cs created with MonoDevelop
// User: alan at 12:18 13/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Runtime.InteropServices;
namespace zipsharp
{
static class NativeZip
{
const int DEFAULT_COMPRESSION = 0;
const int Z_DEFLATED = 8;
public static void CloseArchive (ZipHandle handle)
{
CloseArchive (handle, null);
}
public static void CloseArchive (ZipHandle handle, string comment)
{
zipClose (handle, comment);
handle.SetHandleAsInvalid ();
}
public static void CloseFile (ZipHandle handle)
{
zipCloseFileInZip (handle);
}
public static ZipHandle OpenArchive (ZlibFileFuncDef funcDef, Append append)
{
ZipHandle h = zipOpen2 ("", (int) append, IntPtr.Zero, ref funcDef);
if (h.IsInvalid)
throw new Exception ("Could not open the zip archive");
return h;
}
public static int OpenFile (ZipHandle handle, string filename)
{
return OpenFile (handle, filename, DEFAULT_COMPRESSION);
}
public static int OpenFile (ZipHandle handle, string filename, int compressionLevel)
{
ZipFileInfo fileInfo = new ZipFileInfo (DateTime.Now);
int method = compressionLevel == 0 ? 0 : Z_DEFLATED;
return zipOpenNewFileInZip (handle, filename, ref fileInfo, IntPtr.Zero, 0, IntPtr.Zero, 0, "", method, compressionLevel);
}
public static unsafe void Write (ZipHandle handle, byte[] buffer, int offset, uint count)
{
fixed (byte* b = &buffer[offset])
zipWriteInFileInZip (handle, b, count);
}
[DllImport ("MonoPosixHelper")]
static extern unsafe int zipWriteInFileInZip (ZipHandle handle,
byte* buffer,
uint len);
[DllImport ("MonoPosixHelper")]
static extern int zipCloseFileInZip (ZipHandle handle);
[DllImport ("MonoPosixHelper")]
static extern ZipHandle zipOpen2 (string pathname,
int append,
IntPtr globalcomment, // zipcharpc*
ref ZlibFileFuncDef pzlib_filefunc_def); // zlib_filefunc_def*
[DllImport ("MonoPosixHelper")]
static extern int zipClose (ZipHandle handle, string globalComment);
[DllImport ("MonoPosixHelper")]
static extern int zipOpenNewFileInZip (ZipHandle handle,
string filename,
ref ZipFileInfo zipfi,
IntPtr extrafield_local,
uint size_extrafield_local,
IntPtr extrafield_global,
uint size_extrafield_global,
string comment,
int method,
int level);
}
}

View File

@@ -0,0 +1,104 @@
// UnzipArchive.cs
//
// Copyright (c) 2008 [copyright holders]
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.IO;
namespace zipsharp
{
class UnzipArchive : IDisposable
{
string[] files;
internal bool FileActive {
get; set;
}
string[] Files {
get {
if (files == null)
files = NativeUnzip.GetFiles (Handle);
return files;
}
}
internal UnzipHandle Handle {
get; private set;
}
ZipStream Stream {
get; set;
}
public UnzipArchive (string filename)
: this (File.Open (filename, FileMode.Open), true)
{
}
public UnzipArchive (Stream stream)
: this (stream, false)
{
}
public UnzipArchive (Stream stream, bool ownsStream)
{
Stream = new ZipStream (stream, ownsStream);
Handle = NativeUnzip.OpenArchive (Stream.IOFunctions);
}
public void Dispose ()
{
NativeUnzip.CloseArchive (Handle);
Stream.Close ();
}
public System.IO.Packaging.CompressionOption GetCompressionLevel (string file)
{
using (UnzipReadStream stream = (UnzipReadStream) GetStream (file))
return stream.CompressionLevel;
}
public string[] GetFiles ()
{
return (string []) Files.Clone ();
}
public Stream GetStream (string name)
{
foreach (string file in Files)
{
if (name.Equals(file, StringComparison.OrdinalIgnoreCase))
{
System.IO.Packaging.CompressionOption option;
NativeUnzip.OpenFile (Handle, name, out option);
return new UnzipReadStream (this, option);
}
}
throw new Exception ("The file doesn't exist in the zip archive");
}
}
}

View File

@@ -0,0 +1,112 @@
// FileInfo.cs created with MonoDevelop
// User: alan at 14:47 13/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Runtime.InteropServices;
namespace zipsharp
{
[StructLayout (LayoutKind.Sequential)]
struct UnzipFileInfo
{
IntPtr version; /* version made by 2 bytes */
IntPtr version_needed; /* version needed to extract 2 bytes */
IntPtr flag; /* general purpose bit flag 2 bytes */
IntPtr compression_method; /* compression method 2 bytes */
IntPtr dosDate; /* last mod file date in Dos fmt 4 bytes */
IntPtr crc; /* crc-32 4 bytes */
IntPtr compressed_size; /* compressed size 4 bytes */
IntPtr uncompressed_size; /* uncompressed size 4 bytes */
IntPtr size_filename; /* filename length 2 bytes */
IntPtr size_file_extra; /* extra field length 2 bytes */
IntPtr size_file_comment; /* file comment length 2 bytes */
IntPtr disk_num_start; /* disk number start 2 bytes */
IntPtr internal_fa; /* internal file attributes 2 bytes */
IntPtr external_fa; /* external file attributes 4 bytes */
ZipTime tmu_date;
public ulong VersionNeeded {
get { return (ulong)version_needed.ToInt64 (); }
set { version_needed = new IntPtr ((int)value); }
}
public ulong Version {
get { return (ulong)version.ToInt64 (); }
set { version = new IntPtr ((int)value); }
}
public ulong UncompressedSize {
get { return (ulong)uncompressed_size.ToInt64 (); }
set { uncompressed_size = new IntPtr ((int)value); }
}
public ZipTime TmuDate {
get { return tmu_date; }
set { tmu_date = value; }
}
public ulong SizeFilename {
get { return (ulong)size_filename.ToInt64 (); }
set { size_filename = new IntPtr ((int)value); }
}
public ulong SizeFileExtra {
get { return (ulong)size_file_extra.ToInt64 (); }
set { size_file_extra = new IntPtr ((int)value); }
}
public ulong SizeFileComment {
get {
return (ulong)size_file_comment.ToInt64 ();
}
set {
size_file_comment = new IntPtr ((int)value);
}
}
public ulong InternalFa {
get { return (ulong)internal_fa.ToInt64 (); }
set { internal_fa = new IntPtr ((int)value); }
}
public ulong Flag {
get { return (ulong)flag.ToInt64 (); }
set { flag = new IntPtr ((int)value); }
}
public ulong ExternalFa {
get { return (ulong)external_fa.ToInt64 (); }
set { external_fa = new IntPtr ((int)value); }
}
public ulong DosDate {
get { return (ulong)dosDate.ToInt64 (); }
set { dosDate = new IntPtr ((int)value); }
}
public ulong DiskNumStart {
get { return (ulong)disk_num_start.ToInt64 (); }
set { disk_num_start = new IntPtr ((int)value); }
}
public ulong Crc {
get { return (ulong)crc.ToInt64 (); }
set { crc = new IntPtr ((int)value); }
}
public ulong CompressionMethod {
get { return (ulong)compression_method.ToInt64 (); }
set { compression_method = new IntPtr ((int)value); }
}
public ulong CompressedSize {
get { return (ulong)compressed_size.ToInt64 (); }
set { compressed_size = new IntPtr ((int)value); }
}
}
}

View File

@@ -0,0 +1,32 @@
// UnzipArchive.cs created with MonoDevelop
// User: alan at 13:13 20/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Runtime.InteropServices;
namespace zipsharp
{
class UnzipHandle : SafeHandle
{
public override bool IsInvalid {
get {
return handle == IntPtr.Zero;
}
}
public UnzipHandle ()
: base (IntPtr.Zero, true)
{
}
protected override bool ReleaseHandle ()
{
NativeUnzip.CloseArchive (this);
return true;
}
}
}

View File

@@ -0,0 +1,107 @@
// ZipReadStream.cs
//
// Copyright (c) 2008 [copyright holders]
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.IO;
using System.IO.Packaging;
namespace zipsharp
{
class UnzipReadStream : Stream
{
long length;
UnzipArchive Archive { get; set; }
public override bool CanRead {
get { return true; }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get { return false; }
}
public override bool CanTimeout {
get { return false; }
}
public CompressionOption CompressionLevel {
get; set;
}
public override long Length {
get {
return length;
}
}
public override long Position {
get { return NativeUnzip.CurrentFilePosition (Archive.Handle); }
set { throw new NotSupportedException (); }
}
public UnzipReadStream (UnzipArchive archive, CompressionOption compressionLevel)
{
Archive = archive;
Archive.FileActive = true;
CompressionLevel = compressionLevel;
length = NativeUnzip.CurrentFileLength (Archive.Handle);
}
public override void Close()
{
Archive.FileActive = false;
NativeUnzip.CloseCurrentFile (Archive.Handle);
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int count)
{
return NativeUnzip.Read (Archive.Handle, buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException ();
}
public override void SetLength(long value)
{
throw new NotSupportedException ();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException ();
}
}
}

View File

@@ -0,0 +1,75 @@
// ZipArchive.cs created with MonoDevelop
// User: alan at 16:31 20/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.IO;
using System.IO.Packaging;
namespace zipsharp
{
class ZipArchive : IDisposable
{
internal bool FileActive { get; set; }
internal ZipHandle Handle { get; private set; }
ZipStream Stream { get; set; }
public ZipArchive (string filename, Append append)
: this (File.Open (filename, FileMode.OpenOrCreate), append)
{
}
public ZipArchive (Stream stream, Append append)
: this (stream, append, false)
{
}
public ZipArchive (Stream stream, Append append, bool ownsStream)
{
Stream = new ZipStream (stream, ownsStream);
Handle = NativeZip.OpenArchive (Stream.IOFunctions, append);
}
static int ConvertCompression (System.IO.Packaging.CompressionOption option)
{
switch (option)
{
case CompressionOption.SuperFast:
return 2;
case CompressionOption.Fast:
return 4;
case CompressionOption.Normal:
return 6;
case CompressionOption.Maximum:
return 9;
default:
return 0;
}
}
public Stream GetStream (string filename, System.IO.Packaging.CompressionOption option)
{
if (FileActive)
throw new InvalidOperationException ("A file is already open");
NativeZip.OpenFile (Handle, filename, ConvertCompression (option));
return new ZipWriteStream (this);
}
public void Dispose ()
{
NativeZip.CloseArchive (Handle);
Stream.Close ();
}
}
}

View File

@@ -0,0 +1,47 @@
// ZipFileInfo.cs created with MonoDevelop
// User: alan at 12:14 13/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
namespace zipsharp
{
struct ZipFileInfo
{
ZipTime date;
IntPtr dosDate;
IntPtr internalFileAttributes;
IntPtr externalFileAttributes;
public DateTime FileTime
{
get { return date.Date; }
}
public long DosDate
{
get { return dosDate.ToInt64 (); }
}
internal long InternalFileAttributes
{
get { return internalFileAttributes.ToInt64 (); }
}
internal long ExternalFileAttributes
{
get { return externalFileAttributes.ToInt64 (); }
}
public ZipFileInfo (DateTime fileTime)
{
date = new ZipTime (fileTime);
dosDate = new IntPtr ((int)fileTime.ToFileTime ());
internalFileAttributes = IntPtr.Zero;
externalFileAttributes = IntPtr.Zero;
}
}
}

View File

@@ -0,0 +1,32 @@
// ZipFile.cs created with MonoDevelop
// User: alan at 11:54 13/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Runtime.InteropServices;
namespace zipsharp
{
class ZipHandle : SafeHandle
{
public override bool IsInvalid {
get {
return handle == IntPtr.Zero;
}
}
public ZipHandle ()
: base (IntPtr.Zero, true)
{
}
protected override bool ReleaseHandle()
{
NativeZip.CloseArchive (this);
return true;
}
}
}

View File

@@ -0,0 +1,202 @@
// ZipStream.cs created with MonoDevelop
// User: alan at 15:16 20/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace zipsharp
{
class ZipStream : Stream
{
const int ZLIB_FILEFUNC_SEEK_CUR = 1;
const int ZLIB_FILEFUNC_SEEK_END = 2;
const int ZLIB_FILEFUNC_SEEK_SET = 0;
bool canRead;
bool canSeek;
bool canWrite;
public override bool CanRead {
get { return canRead; }
}
public override bool CanSeek {
get { return canSeek; }
}
public override bool CanWrite {
get { return canWrite; }
}
public override bool CanTimeout {
get { return false; }
}
private Stream DataStream {
get; set;
}
public ZlibFileFuncDef IOFunctions {
get; set;
}
public override long Length {
get { return DataStream.Length; }
}
bool OwnsStream {
get; set;
}
public override long Position {
get { return DataStream.Position; }
set { DataStream.Position = value; }
}
public ZipStream (Stream dataStream, bool ownsStream)
{
// FIXME: Not necessarily true
canRead = true;
canSeek = true;
canWrite = true;
DataStream = dataStream;
OwnsStream = ownsStream;
ZlibFileFuncDef f = new ZlibFileFuncDef();
f.opaque = IntPtr.Zero;
f.zclose_file = CloseFile_Native;
f.zerror_file = TestError_Native;
f.zopen_file = OpenFile_Native;
f.zread_file = ReadFile_Native;
f.zseek_file = SeekFile_Native;
f.ztell_file = TellFile_Native;
f.zwrite_file = WriteFile_Native;
IOFunctions = f;
}
protected override void Dispose(bool disposing)
{
if (!disposing)
return;
DataStream.Flush ();
if (OwnsStream)
DataStream.Dispose ();
}
public override void Flush()
{
DataStream.Flush ();
}
public override int Read(byte[] buffer, int offset, int count)
{
return DataStream.Read (buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
DataStream.Seek (offset, origin);
return DataStream.Position;
}
public override void SetLength(long value)
{
DataStream.SetLength (value);
}
public override void Write(byte[] buffer, int offset, int count)
{
DataStream.Write (buffer, offset, count);
Flush ();
}
int CloseFile_Native (IntPtr opaque, IntPtr stream)
{
Close ();
return 0;
}
IntPtr OpenFile_Native (IntPtr opaque, string filename, int mode)
{
// always success. The stream is opened in managed code
return new IntPtr (1);
}
unsafe IntPtr ReadFile_Native (IntPtr opaque, IntPtr stream, IntPtr buffer, IntPtr size)
{
int count = size.ToInt32 ();
byte[] b = new byte[count];
int read;
try {
read = Math.Max (0, Read (b, 0, count));
byte* ptrBuffer = (byte*) buffer.ToPointer ();
for (int i = 0; i < count && i < read; i ++)
ptrBuffer[i] = b[i];
} catch {
read = -1;
}
return new IntPtr (read);
}
IntPtr SeekFile_Native (IntPtr opaque, IntPtr stream, IntPtr offset, int origin)
{
SeekOrigin seek;
if (origin == ZipStream.ZLIB_FILEFUNC_SEEK_CUR)
seek = SeekOrigin.Current;
else if (origin == ZLIB_FILEFUNC_SEEK_END)
seek = SeekOrigin.End;
else if (origin == ZLIB_FILEFUNC_SEEK_SET)
seek = SeekOrigin.Begin;
else
return new IntPtr (-1);
Seek (offset.ToInt64 (), seek);
return new IntPtr (0);
}
IntPtr TellFile_Native (IntPtr opaque, IntPtr stream)
{
if (IntPtr.Size == 4)
return new IntPtr ((int)Position);
else if (IntPtr.Size == 8)
return new IntPtr (Position);
else
return new IntPtr (-1);
}
int TestError_Native (IntPtr opaque, IntPtr stream)
{
// No errors here.
return 0;
}
unsafe IntPtr WriteFile_Native (IntPtr opaque, IntPtr stream, IntPtr buffer, /* ulong */ IntPtr size)
{
int count = size.ToInt32 ();
byte[] b = new byte[count];
byte* ptrBuffer = (byte*) buffer.ToPointer ();
for (int i = 0; i < count; i ++)
b[i] = ptrBuffer[i];
try {
Write (b, 0, count);
} catch {
}
return new IntPtr (count);
}
}
}

View File

@@ -0,0 +1,37 @@
// ZipTime.cs created with MonoDevelop
// User: alan at 11:56 13/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Runtime.InteropServices;
namespace zipsharp
{
[StructLayoutAttribute (LayoutKind.Sequential)]
struct ZipTime
{
uint second;
uint minute;
uint hour;
uint day;
uint month;
uint year;
public ZipTime (DateTime time)
{
second = (uint) time.Second;
minute = (uint) time.Minute;
hour = (uint) time.Hour;
day = (uint) time.Day;
month = (uint) time.Month;
year = (uint) time.Year;
}
public DateTime Date
{
get { return new DateTime ((int) year, (int) month, (int) day, (int) hour, (int) minute, (int) second); }
}
}
}

View File

@@ -0,0 +1,84 @@
// ZipWriteStream.cs created with MonoDevelop
// User: alan at 16:54 20/10/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.IO;
namespace zipsharp
{
class ZipWriteStream : Stream
{
ZipArchive Archive { get; set; }
public override bool CanRead {
get { return false; }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get { return true; }
}
public override bool CanTimeout {
get { return false; }
}
public override long Length {
get {
throw new NotSupportedException ();
}
}
public override long Position {
get {
throw new NotSupportedException ();
}
set {
throw new NotSupportedException ();
}
}
public ZipWriteStream (ZipArchive archive)
{
Archive = archive;
Archive.FileActive = true;
}
public override void Close()
{
NativeZip.CloseFile (Archive.Handle);
Archive.FileActive = false;
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException ();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException ();
}
public override void SetLength(long value)
{
throw new NotSupportedException ();
}
public override void Write(byte[] buffer, int offset, int count)
{
NativeZip.Write (Archive.Handle, buffer, offset, (uint)count);
}
}
}