Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ZipFileSystemProvider.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_ANDROID
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 using System.IO.Compression.Zip;
8 using System.Linq;
9 using System.Text.RegularExpressions;
10 using SiliconStudio.Core.Serialization;
11 
12 namespace SiliconStudio.Core.IO
13 {
14  /// <summary>
15  /// A file system implementation for IVirtualFileProvider.
16  /// </summary>
17  public class ZipFileSystemProvider : VirtualFileProviderBase
18  {
19  /// <summary>
20  /// Base path of this provider (every path will be relative to this one).
21  /// </summary>
22  private readonly ZipFile zipFile;
23  private readonly Dictionary<string, ZipFileEntry> zipFileEntries = new Dictionary<string, ZipFileEntry>();
24 
25  public ZipFile ZipFile
26  {
27  get { return zipFile; }
28  }
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="FileSystemProvider" /> class with the given base path.
32  /// </summary>
33  /// <param name="rootPath">The root path of this provider.</param>
34  /// <param name="localBasePath">The path to a local directory where this instance will load the files from.</param>
35  public ZipFileSystemProvider(string rootPath, string zipFilePath) : base(rootPath)
36  {
37  zipFile = new ZipFile(zipFilePath);
38  zipFileEntries = zipFile.GetAllEntries()
39  .Where(x => x.FilenameInZip.StartsWith("assets/data/"))
40  .ToDictionary(x => x.FilenameInZip
41  .Replace("assets/data/", string.Empty),
42  x => x);
43  }
44 
45  public override Stream OpenStream(string url, VirtualFileMode mode, VirtualFileAccess access, VirtualFileShare share = VirtualFileShare.Read, StreamFlags streamType = StreamFlags.None)
46  {
47  ZipFileEntry zipFileEntry;
48  if (!zipFileEntries.TryGetValue(url, out zipFileEntry))
49  throw new FileNotFoundException("File not found inside ZIP archive.");
50 
51  if (mode != VirtualFileMode.Open || access != VirtualFileAccess.Read)
52  throw new UnauthorizedAccessException("ZIP archive are read-only.");
53 
54  lock (zipFile)
55  {
56  if (zipFileEntry.Method == Compression.Store)
57  {
58  // Open a VirtualFileStream on top of Zip FileStream
59  return new VirtualFileStream(new FileStream(zipFileEntry.ZipFileName, FileMode.Open, FileAccess.Read), zipFileEntry.FileOffset, zipFileEntry.FileOffset + zipFileEntry.FileSize);
60  }
61 
62  // Decompress it into a MemoryStream
63  var buffer = new byte[zipFileEntry.FileSize];
64  zipFile.ExtractFile(zipFileEntry, buffer);
65  return new MemoryStream(buffer);
66  }
67  }
68 
69  public override bool DirectoryExists(string url)
70  {
71  if(url == null)
72  throw new ArgumentNullException("url");
73 
74  // ensure that the provided path ends by a slash
75  if (!url.EndsWith("/"))
76  url += "/";
77 
78  return zipFileEntries.Any(x => x.Key.StartsWith(url));
79  }
80 
81  public override bool FileExists(string url)
82  {
83  return zipFileEntries.ContainsKey(url);
84  }
85 
86  public override long FileSize(string url)
87  {
88  ZipFileEntry zipFileEntry;
89  if (!zipFileEntries.TryGetValue(url, out zipFileEntry))
90  throw new FileNotFoundException("File not found inside ZIP archive.");
91 
92  return zipFileEntry.FileSize;
93  }
94 
95  public override string[] ListFiles(string url, string searchPattern, VirtualSearchOption searchOption)
96  {
97  url = Regex.Escape(url);
98  searchPattern = Regex.Escape(searchPattern).Replace(@"\*", "[^/]*").Replace(@"\?", "[^/]");
99  var recursivePattern = searchOption == VirtualSearchOption.AllDirectories ? "(.*/)*" : "/?";
100  var regex = new Regex("^" + url + recursivePattern + searchPattern + "$");
101 
102  return zipFileEntries.Keys.Where(x => regex.IsMatch(x)).ToArray();
103  }
104  }
105 }
106 #endif
System.IO.FileMode FileMode
Definition: ScriptSync.cs:33
VirtualFileShare
File share capabilities, equivalent of System.IO.FileShare.
VirtualFileAccess
File access equivalent of System.IO.FileAccess.
VirtualFileMode
File mode equivalent of System.IO.FileMode.
Compression
Compression method enumeration
Definition: Compression.cs:20
StreamFlags
Describes the different type of streams.
Definition: StreamFlags.cs:11