Exemplo n.º 1
0
 public void Add(CoinStack stack)
 {
     Contents.Add(stack);
     RemoveCounterfeitCoins();
     onSafeContentChanged(new EventArgs());
     Save();
 }
Exemplo n.º 2
0
        private void ParseCloudCoinFile(string fullPath)
        {
            try
            {
                FI = new FileInfo(fullPath);
                if (FI.Exists)
                {
                    Filename = fullPath;
                    using (Stream fsSource = FI.Open(FileMode.Open, FileAccess.Read))
                    {
                        byte[] signature = new byte[20];
                        fsSource.Read(signature, 0, 20);
                        string sig = Encoding.UTF8.GetString(signature);
                        var    reg = new Regex(@"{[.\n\t\s\x09\x0A\x0D]*""cloudcoin""");
                        IsValidFile = false;

                        if (Enumerable.SequenceEqual(signature.Take(3), new byte[] { 255, 216, 255 }))                         //JPEG
                        {
                            Filetype = Type.jpeg;
                            var coin = ReadJpeg(fsSource);
                            IsValidFile = coin.Validate();

                            if (coin != null && IsValidFile)
                            {
                                Coins.Add(new CoinStack(coin));
                            }
                        }
                        else if (reg.IsMatch(sig))                         //JSON
                        {
                            Filetype = Type.json;
                            var json = ReadJson(fsSource);

                            if (json != null)
                            {
                                foreach (var coin in json)
                                {
                                    if (!coin.Validate())
                                    {
                                        IsValidFile = false;
                                        break;
                                    }
                                    else
                                    {
                                        IsValidFile = true;
                                    }
                                }
                            }

                            if (IsValidFile)
                            {
                                Coins.Add(json);
                            }
                        }
                        else if (Path.GetExtension(fullPath).Equals((".zip")))
                        {
                            var zip = new ZipArchive();
                            var documentDirectory = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0].Path;

                            zip.UnzipOpenFile(fullPath, "1");

                            zip.UnzipFileTo(documentDirectory + "/unzip", true);

                            zip.OnError += (sender, e) => {
                                Console.WriteLine("Error while unzipping: {0}", e);
                            };

                            Console.WriteLine(zip.UnzippedFiles.Count());
                            zip.UnzipCloseFile();
                        }
                    }

                    //***
                    //if (IsValidFile)
                    //FileSystem.CopyOriginalFileToImported(FI);
                }
                else
                {
                    IsValidFile = false;
                    //throw new FileNotFoundException();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }