示例#1
0
        public static async Task UnzipArchive(Stream inStream, string destinationFolder)
        {
            ICrossPlatformSpecialFolder specialFolder = DependencyService.Get <ICrossPlatformSpecialFolder>();
            string tempFolder = specialFolder.GetTemporaryFolder();
            string zipFile    = Path.Combine(tempFolder, "temp" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".zip");

            try
            {
                FileStream file = File.Create(zipFile);
                try
                {
                    await inStream.CopyToAsync(file);
                }
                finally
                {
                    file.Dispose();
                }
                UnzipArchive(zipFile, destinationFolder);
            }
            finally
            {
                if (File.Exists(zipFile))
                {
                    File.Delete(zipFile);
                }
            }
        }
示例#2
0
        public static async Task ZipArchive(Stream outStream, string folderToArchive)
        {
            ICrossPlatformSpecialFolder specialFolder = DependencyService.Get <ICrossPlatformSpecialFolder>();
            string tempFolder = specialFolder.GetTemporaryFolder();
            string zipFile    = Path.Combine(tempFolder, "temp" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".zip");

            try
            {
                ZipFile.CreateFromDirectory(folderToArchive, zipFile, CompressionLevel.Optimal, false);
                var stream = File.OpenRead(zipFile);
                try
                {
                    await stream.CopyToAsync(outStream);
                }
                finally
                {
                    stream.Dispose();
                }
            }
            finally
            {
                if (File.Exists(zipFile))
                {
                    File.Delete(zipFile);
                }
            }
        }
        /// <summary>
        /// Esegue il ripristino di un backup.
        /// </summary>
        /// <param name="backupFilePath">Percorso del file zip di backup</param>
        public void RestoreFromFile(string backupFilePath)
        {
            ICrossPlatformSpecialFolder specialFolder = DependencyService.Get <ICrossPlatformSpecialFolder>();
            string tempFolder = specialFolder.GetTemporaryFolder();
            string tempExport = Path.Combine(tempFolder, "zip_restore_" + DateTime.Now.ToString("ddMMyyyy_HHmmss"));

            Directory.CreateDirectory(tempExport);

            try
            {
                ArchiveUtility.UnzipArchive(backupFilePath, tempExport);
                DoRestore(tempExport);
            }
            finally
            {
                if (Directory.Exists(tempExport))
                {
                    Directory.Delete(tempExport, true);
                }
            }
        }
        /// <summary>
        /// Effettua il backup dei file e delle cartelle specificate, in formato ZIP.
        /// </summary>
        /// <param name="fileStream">Stream del file ZIP con il backup dei file e delle cartelle</param>
        /// <returns>Dimensione in byte del file di backup</returns>
        public async Task <long> BackupToFile(Stream fileStream)
        {
            ICrossPlatformSpecialFolder specialFolder = DependencyService.Get <ICrossPlatformSpecialFolder>();
            string tempFolder    = specialFolder.GetTemporaryFolder();
            string tempExport    = Path.Combine(tempFolder, DateTime.Now.ToString("ddMMyyyy_HHmmss"));
            string tempZIPFolder = Path.Combine(tempExport, "zip");

            Directory.CreateDirectory(tempZIPFolder);

            try
            {
                foreach (string filePath in Files.Keys)
                {
                    string newPath = Path.Combine(tempZIPFolder, Path.GetFileName(filePath));
                    File.Copy(filePath, newPath, true);
                }
                foreach (string folderPath in Folders.Keys)
                {
                    string newFolder = Path.Combine(tempZIPFolder, Path.GetFileName(folderPath));
                    Directory.CreateDirectory(newFolder);
                    string[] files = Directory.GetFiles(folderPath);
                    foreach (string filePath in files)
                    {
                        string newPath = Path.Combine(newFolder, Path.GetFileName(filePath));
                        File.Copy(filePath, newPath, true);
                    }
                }
                await ArchiveUtility.ZipArchive(fileStream, tempZIPFolder);
            }
            finally
            {
                if (Directory.Exists(tempExport))
                {
                    Directory.Delete(tempExport, true);
                }
            }
            return(fileStream.Length);
        }