1 // Native.cs created with MonoDevelop
2 // User: alan at 12:18 13/10/2008
3 //
4 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
5 //
6 
7 
8 using System;
9 using System.Runtime.InteropServices;
10 
11 namespace zipsharp
12 {
13 	static class NativeZip
14 	{
15 		const int DEFAULT_COMPRESSION = 0;
16 		const int Z_DEFLATED = 8;
17 
CloseArchive(ZipHandle handle)18 		public static void CloseArchive (ZipHandle handle)
19 		{
20 			CloseArchive (handle, null);
21 		}
22 
CloseArchive(ZipHandle handle, string comment)23 		public static void CloseArchive (ZipHandle handle, string comment)
24 		{
25 			zipClose (handle, comment);
26 			handle.SetHandleAsInvalid ();
27 		}
28 
CloseFile(ZipHandle handle)29 		public static void CloseFile (ZipHandle handle)
30 		{
31 			zipCloseFileInZip (handle);
32 		}
33 
OpenArchive32(ZlibFileFuncDef32 funcDef, Append append)34 		public static ZipHandle OpenArchive32 (ZlibFileFuncDef32 funcDef, Append append)
35 		{
36 			ZipHandle h = zipOpen2_32 ("", (int) append, IntPtr.Zero, ref funcDef);
37 			if (h.IsInvalid)
38 				throw new Exception ("Could not open the zip archive");
39 			return h;
40 		}
41 
OpenArchive64(ZlibFileFuncDef64 funcDef, Append append)42 		public static ZipHandle OpenArchive64 (ZlibFileFuncDef64 funcDef, Append append)
43 		{
44 			ZipHandle h = zipOpen2_64 ("", (int) append, IntPtr.Zero, ref funcDef);
45 			if (h.IsInvalid)
46 				throw new Exception ("Could not open the zip archive");
47 			return h;
48 		}
49 
OpenFile32(ZipHandle handle, string filename)50 		public static int OpenFile32 (ZipHandle handle, string filename)
51 		{
52 			return OpenFile32 (handle, filename, DEFAULT_COMPRESSION);
53 		}
54 
OpenFile32(ZipHandle handle, string filename, int compressionLevel)55 		public static int OpenFile32 (ZipHandle handle, string filename, int compressionLevel)
56 		{
57 			ZipFileInfo32 fileInfo = new ZipFileInfo32 (DateTime.Now);
58 			int method = compressionLevel == 0 ? 0 : Z_DEFLATED;
59 			return zipOpenNewFileInZip_32 (handle, filename, ref fileInfo, IntPtr.Zero, 0, IntPtr.Zero, 0, "", method, compressionLevel);
60 		}
61 
OpenFile64(ZipHandle handle, string filename)62 		public static int OpenFile64 (ZipHandle handle, string filename)
63 		{
64 			return OpenFile64 (handle, filename, DEFAULT_COMPRESSION);
65 		}
66 
OpenFile64(ZipHandle handle, string filename, int compressionLevel)67 		public static int OpenFile64 (ZipHandle handle, string filename, int compressionLevel)
68 		{
69 			ZipFileInfo64 fileInfo = new ZipFileInfo64 (DateTime.Now);
70 			int method = compressionLevel == 0 ? 0 : Z_DEFLATED;
71 			return zipOpenNewFileInZip_64 (handle, filename, ref fileInfo, IntPtr.Zero, 0, IntPtr.Zero, 0, "", method, compressionLevel);
72 		}
73 
Write(ZipHandle handle, byte[] buffer, int offset, uint count)74 		public static unsafe void Write (ZipHandle handle, byte[] buffer, int offset, uint count)
75 		{
76 			fixed (byte* b = &buffer[offset])
77 				zipWriteInFileInZip (handle, b, count);
78 		}
79 
80 		[DllImport ("MonoPosixHelper")]
zipWriteInFileInZip(ZipHandle handle, byte* buffer, uint len)81 		static extern unsafe int zipWriteInFileInZip (ZipHandle handle,
82 		                                               byte* buffer,
83 		                                               uint len);
84 
85 		[DllImport ("MonoPosixHelper")]
zipCloseFileInZip(ZipHandle handle)86 		static extern int zipCloseFileInZip (ZipHandle handle);
87 
88 		[DllImport ("MonoPosixHelper", EntryPoint = "zipOpen2")]
zipOpen2_32(string pathname, int append, IntPtr globalcomment, ref ZlibFileFuncDef32 pzlib_filefunc_def)89 		static extern ZipHandle zipOpen2_32 (string pathname,
90 		                                     int append,
91 		                                     IntPtr globalcomment, // zipcharpc*
92 		                                     ref ZlibFileFuncDef32 pzlib_filefunc_def); // zlib_filefunc_def*
93 
94 		[DllImport ("MonoPosixHelper", EntryPoint = "zipOpen2")]
zipOpen2_64(string pathname, int append, IntPtr globalcomment, ref ZlibFileFuncDef64 pzlib_filefunc_def)95 		static extern ZipHandle zipOpen2_64 (string pathname,
96 		                                     int append,
97 		                                     IntPtr globalcomment, // zipcharpc*
98 		                                     ref ZlibFileFuncDef64 pzlib_filefunc_def); // zlib_filefunc_def*
99 
100 		[DllImport ("MonoPosixHelper")]
zipClose(ZipHandle handle, string globalComment)101 		static extern int zipClose (ZipHandle handle, string globalComment);
102 
103 		[DllImport ("MonoPosixHelper", EntryPoint = "zipOpenNewFileInZip")]
zipOpenNewFileInZip_32(ZipHandle handle, string filename, ref ZipFileInfo32 zipfi, IntPtr extrafield_local, uint size_extrafield_local, IntPtr extrafield_global, uint size_extrafield_global, string comment, int method, int level)104 		static extern int zipOpenNewFileInZip_32 (ZipHandle handle,
105 		                                          string filename,
106 		                                          ref ZipFileInfo32 zipfi,
107 		                                          IntPtr extrafield_local,
108 		                                          uint size_extrafield_local,
109 		                                          IntPtr extrafield_global,
110 		                                          uint size_extrafield_global,
111 		                                          string comment,
112 		                                          int method,
113 		                                          int level);
114 
115 		[DllImport ("MonoPosixHelper", EntryPoint = "zipOpenNewFileInZip")]
zipOpenNewFileInZip_64(ZipHandle handle, string filename, ref ZipFileInfo64 zipfi, IntPtr extrafield_local, uint size_extrafield_local, IntPtr extrafield_global, uint size_extrafield_global, string comment, int method, int level)116 		static extern int zipOpenNewFileInZip_64 (ZipHandle handle,
117 		                                          string filename,
118 		                                          ref ZipFileInfo64 zipfi,
119 		                                          IntPtr extrafield_local,
120 		                                          uint size_extrafield_local,
121 		                                          IntPtr extrafield_global,
122 		                                          uint size_extrafield_global,
123 		                                          string comment,
124 		                                          int method,
125 		                                          int level);
126 	}
127 }
128