Simple unzip utility leveraging Silverlight's GetResourceStream
Inheritance: IDisposable
示例#1
0
        /// <summary>
        /// Processes each file in the ZIP stream, storing images in a dictionary and load the KML contents
        /// into an XDocument.
        /// </summary>
        /// <param name="zipFile">Decompressed stream from KMZ.</param>
        /// <returns>XDocument containing KML content from the KMZ source.</returns>
        private XDocument GetKmzContents(ZipFile zipFile)
        {
            XDocument xDoc = null;

            // Process each file in the archive
            foreach (string filename in zipFile.EntryFileNames)
            {
                // Determine where the last "." character exists in the filename and if is does not appear
                // at all, then skip the file.
                int lastPeriod = filename.LastIndexOf(".");
                if (lastPeriod == -1)
                    continue;

            #if SILVERLIGHT
                System.IO.Stream ms = zipFile.GetFileStream(filename);
            #else
                MemoryStream ms = new MemoryStream();
                zipFile.Extract(filename, ms);
            #endif
                if (ms == null) continue;
                ms.Seek(0, SeekOrigin.Begin);

                switch (filename.Substring(lastPeriod).ToLower())
                {
                    case ".jpg":
                    case ".jpeg":
                    case ".png":
            #if !SILVERLIGHT
                    case ".bmp":
                    case ".gif":
            #endif
                        // If the file is an image, then add it to the dictionary of images and use
                        // its filename as the key since this will match the subsequent KML style
                        // information for point features.
                        try
                        {
                            BitmapImage thumbnailBitmap = new BitmapImage();
            #if SILVERLIGHT
                            thumbnailBitmap.SetSource(ms);
            #else
                            thumbnailBitmap.BeginInit();
                            thumbnailBitmap.StreamSource = ms;
                            thumbnailBitmap.EndInit();
            #endif
                            ImageBrush ib = new ImageBrush();
                            ib.ImageSource = thumbnailBitmap;
                            _context.Images.Add(filename.ToLower(), ib);
                        }
                        catch { }

                        break;

                    case ".kml":
                        // Create the XDocument object from the input stream
                        xDoc = LoadDocument(ms);
                        break;

                }
            }

            return xDoc;
        }
		private static void StoreZipfileAndCallback(KMLStyle kmlStyle, Action<KMLStyle> callback, ZipFile zipFile)
		{
			if (zipFile != null)
			{
				if (kmlStyle.ZipFile == null && !String.IsNullOrEmpty(kmlStyle.IconHref))
				{
					kmlStyle.ZipFile = zipFile;
				}
				else
				{
					zipFile.Dispose();
				}
			}
			callback(kmlStyle);
		}
		/// <summary>
		/// Processes each file in the ZIP stream, storing images in a dictionary and load the KML contents
		/// into an XDocument.
		/// </summary>
		/// <param name="zipFile">Decompressed stream from KMZ.</param>
		/// <returns>XDocument containing KML content from the KMZ source.</returns>
		private static XDocument GetKmzContents(ZipFile zipFile)
		{
			XDocument xDoc = null;

			// Process each file in the archive
			foreach (string filename in zipFile.EntryFileNames)
			{
				// Determine where the last "." character exists in the filename and if is does not appear
				// at all, then skip the file.
				int lastPeriod = filename.LastIndexOf(".");
				if (lastPeriod == -1)
					continue;

#if SILVERLIGHT
				Stream ms = zipFile.GetFileStream(filename);
#else
				MemoryStream ms = new MemoryStream();
				zipFile.Extract(filename, ms);
#endif
				if (ms == null) continue;
				ms.Seek(0, SeekOrigin.Begin);

				switch (filename.Substring(lastPeriod).ToLower())
				{
					case ".kml":
						// Create the XDocument object from the input stream
						try
						{
#if SILVERLIGHT
							xDoc = XDocument.Load(ms, LoadOptions.None);
#else
							xDoc = XDocument.Load(XmlReader.Create(ms));
#endif
						}
						catch
						{
							xDoc = null;
						}
						break;
				}
			}

			return xDoc;
		}
        private ImageBrush GetIconImage(ZipFile zipFile, string imageName)
        {
            // Look for a filename equals to imageName
            string filename = zipFile.EntryFileNames.Where(n => n.ToLower() == imageName).FirstOrDefault();
            if (filename == null)
                return null;

#if SILVERLIGHT
            System.IO.Stream ms = zipFile.GetFileStream(filename);
#else
			MemoryStream ms = new MemoryStream();
			zipFile.Extract(filename, ms);
#endif
            if (ms == null)
                return null;
            ms.Seek(0, SeekOrigin.Begin);

            ImageBrush ib = null;
            try
            {
                BitmapImage thumbnailBitmap = new BitmapImage();
#if SILVERLIGHT
                thumbnailBitmap.SetSource(ms);
#else
                thumbnailBitmap.BeginInit();
                thumbnailBitmap.StreamSource = ms;
                thumbnailBitmap.EndInit();
#endif
                ib = new ImageBrush {ImageSource = thumbnailBitmap};
            }
            catch { ib = null;}

            return ib;
        }