Пример #1
0
        /// <summary>
        /// Create a new PebbleBundle from a .pwb file and parse its metadata.
        /// </summary>
        /// <param name="path">The relative or full path to the file.</param>
        public P3bbleBundle(String path)
        {
            Stream jsonstream;
            Stream binstream;

            _privateName = path;

            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

            if (!file.FileExists(path))
            {
                throw new FileNotFoundException("the file could not be found in the isolated storage");
            }

            FullPath = Path.GetFullPath(path);
            Bundle   = new UnZipper(file.OpenFile(path, FileMode.Open));

            if (Bundle.FileNamesInZip.Contains("manifest.json"))
            {
                jsonstream = Bundle.GetFileStream("manifest.json");
            }
            else
            {
                throw new ArgumentException("manifest.json not found in archive - not a Pebble bundle.");
            }

            var serializer = new DataContractJsonSerializer(typeof(P3bbleBundleManifest));


            Manifest = serializer.ReadObject(jsonstream) as P3bbleBundleManifest;
            jsonstream.Close();

            HasResources = (Manifest.Resources.Size != 0);

            if (Manifest.Type == "firmware")
            {
                BundleType = BundleTypes.Firmware;
            }
            else
            {
                BundleType = BundleTypes.Application;
                if (Bundle.FileNamesInZip.Contains(Manifest.Application.Filename))
                {
                    binstream = Bundle.GetFileStream(Manifest.Application.Filename);
                }
                else
                {
                    String format = "App file {0} not found in archive";
                    throw new ArgumentException(String.Format(format, Manifest.Application.Filename));
                }

                Application = Util.ReadStruct <P3bbleApplicationMetadata>(binstream);
                binstream.Close();
            }
        }
Пример #2
0
        private void Unzip(Stream stream)
        {
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var zipStream = new UnZipper(stream))
                {
                    foreach (string file in zipStream.FileNamesInZip)
                    {
                        string fileName = System.IO.Path.GetFileName(file);

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            Debug.WriteLine("Extracting " + fileName + " ...");

                            //save file entry to storage
                            using (var streamWriter =
                                       new BinaryWriter(new IsolatedStorageFileStream(fileName,
                                                                                      FileMode.Create,
                                                                                      FileAccess.Write, FileShare.Write,
                                                                                      isoStore)))
                            {
                                Stream fileStream = zipStream.GetFileStream(file);

                                var buffer = new byte[2048];
                                int size;
                                while ((size = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    streamWriter.Write(buffer, 0, size);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        //public static Level GetDownloadedLevel(string levelDirectoryName)
        //{
        //    string rootPckPath = PACKAGES_FOLDER_PATH + Path.DirectorySeparatorChar + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName + Path.DirectorySeparatorChar;

        //    using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
        //    {
        //        if (levelDirectoryName.StartsWith(LEVEL_PREFIX) && isoStore.FileExists(rootPckPath + levelDirectoryName + Path.DirectorySeparatorChar + levelDirectoryName + GameDBHelper.DB_FILE_NAME_EXTENSION))
        //        {
        //            string dbPath = rootPckPath + levelDirectoryName + Path.DirectorySeparatorChar + levelDirectoryName + GameDBHelper.DB_FILE_NAME_EXTENSION;
        //            return GetDownloadedLevelFromDbPath(dbPath);


        //            //using (PackageDal dal = new PackageDal(rootPckPath + packDirectoryName + Path.DirectorySeparatorChar + packDirectoryName + PackageDal.DB_FILE_NAME_EXTENSION))
        //            //{
        //            //    if (dal.OpenDatabase())
        //            //    {
        //            //        return dal.GetPackage();
        //            //    }
        //            //}
        //        }
        //    }
        //    return null;
        //}


        //public static Level GetDownloadedLevelFromDbPath(string fullDataBasePath)
        //{
        //    using (LevelDBHelper dal = new LevelDBHelper(fullDataBasePath))
        //    {
        //        if (dal.OpenDatabase())
        //        {
        //            var levels = dal.GetLevels();
        //            if (levels == null || levels.Count == 0)
        //                return null;

        //            return levels.First().Value;
        //        }
        //    }
        //    return null;
        //}


        private static void ExtractZipFile(string archiveFilenameIn, string password, string outFolderPath)
        {
            using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                Stream stream = null;
                try
                {
                    using (UnZipper unzip = new UnZipper(isoStore.OpenFile(archiveFilenameIn, FileMode.Open)))
                    {
                        foreach (string filename in unzip.FileNamesInZip)
                        {
                            stream = unzip.GetFileStream(filename);
                            string correctedFilename = filename.Replace("/", "" + Path.DirectorySeparatorChar);
                            IsolatedStorageHelpers.SaveFileAndCreateParentFolders(outFolderPath + Path.DirectorySeparatorChar + correctedFilename, stream);
                        }
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close(); // Ensure we release resources
                    }
                }
            }
        }
Пример #4
0
        public static async Task <string> unzipToIsolatedStoreLocSharpGISLib()
        {
            try
            {
                //  delDirIsoStorBeforeUnzip("www");
                IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
                using (userStoreForApplication)
                {
                    using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("tempUpdate.zip", FileMode.Open, userStoreForApplication))
                    {
                        using (UnZipper unZipper = new UnZipper(isolatedStorageFileStream))
                        {
                            using (IEnumerator <string> enumerator = unZipper.FileNamesInZip.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    string        current       = enumerator.Current;
                                    string        fileName      = Path.GetFileName(current);
                                    StorageFolder localFolder   = ApplicationData.Current.LocalFolder;
                                    string        directoryName = Path.GetDirectoryName(current);
                                    if (!string.IsNullOrEmpty(fileName))
                                    {
                                        if (!Directory.Exists(localFolder.Path + "\\" + directoryName))
                                        {
                                            // WLUtils.LOG("Directory Created .... \\www\\" + directoryName);
                                            await localFolder.CreateFolderAsync(directoryName, CreationCollisionOption.OpenIfExists);
                                        }
                                        using (BinaryWriter binaryWriter = new BinaryWriter(await localFolder.OpenStreamForWriteAsync(directoryName + "\\" + fileName, CreationCollisionOption.ReplaceExisting)))
                                        {
                                            //WLUtils.LOG("Path.GetDirectoryName " + directoryName);
                                            //WLUtils.LOG("filename is www\\" + directoryName + "\\" + fileName);
                                            Stream fileStream = unZipper.GetFileStream(current);
                                            byte[] array      = new byte[1028];
                                            if (fileStream != null)
                                            {
                                                int num;
                                                while ((num = fileStream.Read(array, 0, array.Length)) > 0)
                                                {
                                                    binaryWriter.Write(array, 0, num);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //   Debugger.Log(0, null, messages.getMessage("WLDirectUpdate.UNZIP_FAILED" + ex.StackTrace));
                // WLUtils.LOG("unzipAndSaveFilesInInstalledLoc (IsolatedStorageException) failed  .... ", ex);

                return("Error:" + ex.Message);
            }

            return("OK");
        }
Пример #5
0
        public static XElement GetXLSXPart(this UnZipper unzip, string xmlName)
        {
            XElement partElement = null;

            using (Stream partStream = unzip.GetFileStream(xmlName))
            {
                partElement = XElement.Load(XmlReader.Create(partStream));
            }
            return(partElement);
        }
Пример #6
0
        public static async Task <HttpResponseMessage> GetTiles(string buid, string floor_number)
        {
            var r = new RequestPoisByFloor
            {
                access_token = "api_tester",
                buid         = buid,
                floor_number = floor_number,
                username     = "",
                password     = ""
            };

            try
            {
                var response = await AnyPlaceClient.PostAsJsonAsync(new Uri(ServerBaseUri, "/anyplace/floortiles/zip/" + buid + "/" + floor_number), r);

                if (!response.IsSuccessStatusCode)
                {
                    return(null);
                }

                var x = await response.Content.ReadAsStreamAsync();

                using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var zipStream = new UnZipper(x))
                    {
                        isoStore.CreateDirectory(buid);
                        isoStore.CreateDirectory(buid + "/" + floor_number);

                        foreach (var file in zipStream.FileNamesInZip)
                        {
                            string fileName = Path.GetFileName(file);

                            if (!string.IsNullOrEmpty(fileName))
                            {
                                Debug.WriteLine(fileName);

                                //save file entry to storage
                                using (var streamWriter =
                                           new BinaryWriter(new IsolatedStorageFileStream(buid + "/" + floor_number + "/" + fileName,
                                                                                          FileMode.Create,
                                                                                          FileAccess.Write, FileShare.Write,
                                                                                          isoStore)))
                                {
                                    Stream fileStream = zipStream.GetFileStream(file);

                                    var buffer = new byte[2048];
                                    int size;
                                    while ((size = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        streamWriter.Write(buffer, 0, size);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) {
                var x = e.Message;
            }
            return(null);
        }
Пример #7
0
        public static async Task <HttpResponseMessage> GetTiles(string buid, string floor_number)
        {
            var r = new RequestPoisByFloor
            {
                access_token = "api_tester",
                buid         = buid,
                floor_number = floor_number,
                username     = "",
                password     = ""
            };

            try
            {
                var response = await AnyPlaceClient.PostAsJsonAsync(new Uri(ServerBaseUri, "/anyplace/floortiles/zip/" + buid + "/" + floor_number), r);

                if (!response.IsSuccessStatusCode)
                {
                    return(null);
                }

                var x = await response.Content.ReadAsStreamAsync();

                var install = Windows.ApplicationModel.Package.Current;
                var local   = ApplicationData.Current.LocalFolder;

                using (var zipStream = new UnZipper(x))
                {
                    var dataFolder = await local.CreateFolderAsync("Datafolder", CreationCollisionOption.OpenIfExists);

                    foreach (var file in zipStream.FileNamesInZip)
                    {
                        // var par = await storageFolder.CreateFolderAsync("/data");
                        var    splited  = file.Split('/');
                        var    zoom     = splited[0];
                        string fileName = buid + "." + floor_number + "." + splited[1];

                        var fi = await dataFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                        if (!string.IsNullOrEmpty(fileName))
                        {
                            Debug.WriteLine(fileName);
                            var str = await fi.OpenStreamForWriteAsync();

                            Stream fileStream = zipStream.GetFileStream(file);

                            var buffer = new byte[2048];
                            int size;
                            while ((size = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                str.Write(buffer, 0, size);
                            }
                            fileStream.Close();
                            await str.FlushAsync();

                            str.Close();
                        }
                    }
                }

                using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var zipStream = new UnZipper(x))
                    {
                        isoStore.CreateDirectory(buid);
                        isoStore.CreateDirectory(buid + "/" + floor_number);

                        foreach (var file in zipStream.FileNamesInZip)
                        {
                            string fileName = Path.GetFileName(file);

                            if (!string.IsNullOrEmpty(fileName))
                            {
                                Debug.WriteLine(fileName);

                                //save file entry to storage
                                using (var streamWriter =
                                           new BinaryWriter(new IsolatedStorageFileStream(buid + "/" + floor_number + "/" + fileName,
                                                                                          FileMode.Create,
                                                                                          FileAccess.Write, FileShare.Write,
                                                                                          isoStore)))
                                {
                                    Stream fileStream = zipStream.GetFileStream(file);

                                    var buffer = new byte[2048];
                                    int size;
                                    while ((size = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        streamWriter.Write(buffer, 0, size);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e) {
                var x = e.Message;
            }
            return(null);
        }
Пример #8
0
        /// <summary>
        /// Unzips a given zip file into the path specified.Path can be directory
        /// or file.
        /// throws Exception
        /// </summary>
        /// <param name="ZipFilePath"></param>
        /// <param name="PathToExtract"></param>
        internal static void unzip(string ZipFilePath, string PathToExtract)
        {
            IsolatedStorageFile       storage = null;
            IsolatedStorageFileStream stream  = null;
            string oldfilePath = null;
            string newfilePath = null;

            try
            {
                using (storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (storage.FileExists(ZipFilePath))
                    {
                        // Create target directory, if not exists
                        CreateDirectory(PathToExtract);

                        using (stream = new IsolatedStorageFileStream(ZipFilePath, FileMode.Open,
                                                                      FileAccess.Read, storage))
                        {
                            if (stream != null)
                            {
                                UnZipper unzip = new UnZipper(stream);
                                foreach (string filename in unzip.FileNamesInZip)
                                {
                                    newfilePath = PathToExtract + "\\" + filename;
                                    oldfilePath = ZipFilePath + "\\" + filename;

                                    // Logger.Info("Unzipping  " + newfilePath);

                                    if (filename.EndsWith("/"))
                                    {
                                        newfilePath = newfilePath.Substring(0, newfilePath.LastIndexOf("/"));
                                        storage.CreateDirectory(newfilePath);
                                    }
                                    else
                                    {
                                        //it is a file
                                        if (filename.IndexOf("/") > -1)
                                        {
                                            string dirPath = newfilePath.Substring(0, newfilePath.LastIndexOf("/"));
                                            if (!storage.DirectoryExists(dirPath))
                                            {
                                                storage.CreateDirectory(dirPath);
                                            }
                                        }

                                        byte[] data       = null;
                                        Stream fileStream = unzip.GetFileStream(filename);
                                        if (fileStream != null)
                                        {
                                            using (BinaryReader br = new BinaryReader(fileStream))
                                            {
                                                data = br.ReadBytes((int)fileStream.Length);
                                            }

                                            //Remove if already exists
                                            if (storage.FileExists(newfilePath))
                                            {
                                                storage.DeleteFile(newfilePath);
                                            }

                                            using (BinaryWriter bw = new BinaryWriter(storage.CreateFile(newfilePath)))
                                            {
                                                bw.Write(data);
                                                bw.Close();
                                                bw.Dispose();
                                            };
                                            try
                                            {
                                                fileStream.Close();
                                                fileStream.Dispose();
                                            }
                                            catch { }
                                        }
                                        else
                                        {
                                            Logger.Warn("File is empty: " + filename);
                                            storage.CreateFile(newfilePath);
                                        }
                                    }
                                    // Logger.Info("Done");
                                }
                                try
                                {
                                    stream.Close();
                                    stream.Dispose();

                                    unzip.Dispose();
                                }
                                catch { }
                            }
                            else
                            {
                                string msg = "Empty zip file stream. Reason - Stream is empty";
                                throw new Exception(msg);
                            }
                        }
                    }
                    else
                    {
                        string msg = String.Format("Cannot unzip file. Invalid path : {0}", ZipFilePath);
                        throw new Exception(msg);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error("Unzip failed. Reason - " + e.Message);
                throw e;
            }
            finally
            {
                try
                {
                    storage.Dispose();
                }
                catch { }
            }
        }