将Mxd文件压缩并上传到Geodatabase!

把Mxd文件压缩上传测试代码!

View Code
  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using System.IO.Compression;

  6 using System.IO;

  7 using ESRI.ArcGIS.DataSourcesGDB;

  8 using ESRI.ArcGIS.esriSystem;

  9 using ESRI.ArcGIS.Geodatabase;

 10 using ESRI.ArcGIS.Geometry;

 11 namespace CompressMxd

 12 {

 13     class CompressMxdFile

 14     {

 15         public static IWorkspace OpenWorkspace(string mdbPath)

 16         {

 17             IWorkspaceFactory pwspfact=new AccessWorkspaceFactoryClass();

 18             IWorkspace pwsp = pwspfact.OpenFromFile(mdbPath, 0);

 19             return pwsp;

 20         }

 21         public static bool ReadAndCompressMxdFile(string fileName, IWorkspace pwsp)

 22         {

 23             byte[] buffer = null;

 24             try

 25             {

 26                 using (System.IO.FileStream sourceStream = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))

 27                 {

 28                     buffer = new byte[sourceStream.Length];

 29                     int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);

 30                     byte[] compressBytes = Compress(buffer);

 31 

 32                     MemoryBlobStream blob = new MemoryBlobStream();

 33                     ((IMemoryBlobStreamVariant)blob).ImportFromVariant(compressBytes as object);//将一个Byte[]对象作为参数传入

 34 

 35                     IFeatureWorkspace pFwsp = pwsp as IFeatureWorkspace;

 36                     ITable ptable = pFwsp.OpenTable("MxdCollection");

 37                     IRow row = ptable.CreateRow();

 38                     var idx = row.Fields.FindField("MxdFile");

 39                       IField field = ptable.Fields.get_Field(idx);

 40                       if (field.CheckValue(blob))

 41                       {

 42                           row.set_Value(idx, blob as object);

 43                       }

 44                       row.Store();

 45                 }

 46             }

 47             catch (System.Exception ex)

 48             {

 49                 return false;

 50             }

 51            

 52             return true;

 53         }

 54         public static bool DeCompressMxdFile(string fileName,IWorkspace pwsp)

 55         {

 56             

 57             IFeatureWorkspace pFwsp = pwsp as IFeatureWorkspace;

 58             ITable ptable = pFwsp.OpenTable("MxdCollection");

 59             IRow row = ptable.GetRow(4);

 60             var idx = row.Fields.FindField("MxdFile");

 61             object objtest =  row.get_Value(idx);

 62            

 63             IMemoryBlobStreamVariant pVar = objtest as IMemoryBlobStreamVariant;

 64             object pobj = new object();

 65             pVar.ExportToVariant(out pobj); //转入Object对象

 66             byte[] desbytes = Decompress((byte[])pobj);

 67             using (System.IO.FileStream destinationStream = new System.IO.FileStream(fileName, FileMode.Create))

 68             {

 69                 destinationStream.Write(desbytes,0,desbytes.Length);

 70             }

 71             return true;

 72         }

 73         private static byte[] Compress(byte[] bytes)

 74         {

 75             using (MemoryStream ms = new MemoryStream())

 76             {

 77                 GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);

 78 

 79                 Compress.Write(bytes, 0, bytes.Length);

 80 

 81                 Compress.Close();

 82 

 83                 return ms.ToArray();

 84 

 85             }

 86         }

 87 

 88         private static byte[] Decompress(Byte[] bytes)

 89         {

 90             using (MemoryStream tempMs = new MemoryStream())

 91             {

 92                 using (MemoryStream ms = new MemoryStream(bytes))

 93                 {

 94                     GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);

 95 

 96                     Decompress.CopyTo(tempMs);

 97 

 98                     Decompress.Close();

 99 

100                     return tempMs.ToArray();

101                 }

102             }

103         }

104     }

105 }

  这里的这个思路和代码完全可以将任何文件上传到Geodatabase中!当然文件大小是受限制的。这和Access有关和机器的位数有关,在Oracle中是否可以放开一些文件大小方面的限制?

  对文件进行管理,通过Hash算法进行文件的版本控制!这个想法确实不错,不过具体要做成什么样子呢,我也不知道!

  微软提供了Compress(Gzipstream类)和Security命名空间分别实现了LZ77和哈弗曼编码结合的压缩算法,实现了加密的算法,可以直接调用。

  说实话不太喜欢拿来主义,用是很方便,但是真的不是自己做出来的,感觉理解方面还是有很多困难。不知道其内部机制到底是怎样的!!!

  不知道如果Mxd文档调用的要素类是服务器中的,我的Mxd文档下载到任意客户端是否都能够显示图形要素!

你可能感兴趣的:(database)