public string GetTemporaryFile(string name) { string s = System.IO.Path.Combine(_path, name); RobustFile.Create(s).Close(); return(s); }
public static void ExpandZip(string srcPath, string destFolderPath) { var zipFile = new ZipFile(srcPath); try { byte[] buffer = new byte[4096]; // 4K is optimum foreach (ZipEntry entry in zipFile) { var fullOutputPath = Path.Combine(destFolderPath, entry.Name); if (entry.IsDirectory) { Directory.CreateDirectory(fullOutputPath); // In the SharpZipLib code, IsFile and IsDirectory are not defined exactly as inverse: a third // (or fourth) type of entry might be possible. I don't think this will be an issue in widget files. continue; } var directoryName = Path.GetDirectoryName(fullOutputPath); if (!String.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } using (var instream = zipFile.GetInputStream(entry)) using (var writer = RobustFile.Create(fullOutputPath)) { ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(instream, writer, buffer); } } } finally { zipFile.Close(); } }
public static void CompressDirectory(string outputPath, string directoryToCompress, string dirNamePrefix, bool forReaderTools = false, bool excludeAudio = false, bool reduceImages = false, bool omitMetaJson = false, bool wrapWithFolder = true, string pathToFileForSha = null) { using (var fsOut = RobustFile.Create(outputPath)) { using (var zipStream = new ZipOutputStream(fsOut)) { zipStream.SetLevel(9); int dirNameOffset; if (wrapWithFolder) { // zip entry names will start with the compressed folder name (zip will contain the // compressed folder as a folder...we do this in bloompacks, not sure why). var rootName = Path.GetFileName(directoryToCompress); dirNameOffset = directoryToCompress.Length - rootName.Length; } else { // zip entry names will start with the files or directories at the root of the book folder // (zip root will contain the folder contents...suitable for compressing a single book into // a zip, as with .bloomd files) dirNameOffset = directoryToCompress.Length + 1; } CompressDirectory(directoryToCompress, zipStream, dirNameOffset, dirNamePrefix, forReaderTools, excludeAudio, reduceImages, omitMetaJson, pathToFileForSha); zipStream.IsStreamOwner = true; // makes the Close() also close the underlying stream zipStream.Close(); } } }
public static void UnzipDirectory(string destFolder, string zipPath) { byte[] buffer = new byte[4096]; // 4K is optimum RetryUtility.Retry(() => { using (var zipFile = new ZipFile(zipPath)) { foreach (ZipEntry entry in zipFile) { var fullOutputPath = Path.Combine(destFolder, entry.Name); if (entry.IsDirectory) { Directory.CreateDirectory(fullOutputPath); // In the SharpZipLib code, IsFile and IsDirectory are not defined exactly as inverse: a third // (or fourth) type of entry might be possible. In practice in .bloom files, this should not be // an issue. continue; } var directoryName = Path.GetDirectoryName(fullOutputPath); if (!String.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } using (var instream = zipFile.GetInputStream(entry)) { using (var writer = RobustFile.Create(fullOutputPath)) { StreamUtils.Copy(instream, writer, buffer); } } } } }); }
public BloomZipFile(string path) { var fsOut = RobustFile.Create(path); _zipStream = new ZipOutputStream(fsOut); _zipStream.SetLevel(9); //REVIEW: what does this mean? }
public BloomZipFile(string path) { var fsOut = RobustFile.Create(path); _zipStream = new ZipOutputStream(fsOut); _zipStream.SetLevel(9); // the compression level (9 is the most compression) }
public void AddDirectory_ProgressCallbackSpecified_CorrectProgressReported() { // Setup using (var testFolder = new TemporaryFolder(kTestFolderName)) { var filenames = new string[] { "1.txt", "2.txt", "temp.tmp" }; foreach (var filename in filenames) { var fs = RobustFile.Create(Path.Combine(testFolder.Path, filename)); fs.Dispose(); } var archive = new MockBloomArchiveSubclass(); var extensionsToExclude = new string[] { ".tmp" }; // Check that the count takes into account excluded files var progressUpdates = new List <float>(); Action <float> progressCallback = (progressProportion) => { progressUpdates.Add(progressProportion); }; // System under test archive.AddDirectory(testFolder.Path, testFolder.Path.Length, extensionsToExclude, progressCallback); // Verification CollectionAssert.AreEqual(new float[] { 0.5f, 1f }, progressUpdates); } }
public TempFile GetNewTempFile(bool doCreateTheFile) { string s = System.IO.Path.GetRandomFileName(); s = System.IO.Path.Combine(_path, s); if (doCreateTheFile) { RobustFile.Create(s).Close(); } return(TempFile.TrackExisting(s)); }
public string GetPathForNewTempFile(bool doCreateTheFile, string extension) { extension = extension.TrimStart('.'); var s = System.IO.Path.Combine(_path, System.IO.Path.GetRandomFileName() + "." + extension); if (doCreateTheFile) { RobustFile.Create(s).Close(); } return(s); }
public string GetPathForNewTempFile(bool doCreateTheFile) { string s = System.IO.Path.GetRandomFileName(); s = System.IO.Path.Combine(_path, s); if (doCreateTheFile) { RobustFile.Create(s).Close(); } return(s); }
private void MakeBloomPackInternal(string path, string dir, int dirNameOffset, string dirNamePrefix, bool forReaderTools) { try { if (RobustFile.Exists(path)) { // UI already got permission for this RobustFile.Delete(path); } using (var pleaseWait = new SimpleMessageDialog("Creating BloomPack...", "Bloom")) { try { pleaseWait.Show(); pleaseWait.BringToFront(); Application.DoEvents(); // actually show it Cursor.Current = Cursors.WaitCursor; Logger.WriteEvent("BloomPack path will be " + path + ", made from " + dir + " with rootName " + Path.GetFileName(dir)); using (var fsOut = RobustFile.Create(path)) { using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)) { zipStream.SetLevel(9); CompressDirectory(dir, zipStream, dirNameOffset, dirNamePrefix, forReaderTools); zipStream.IsStreamOwner = true; // makes the Close() also close the underlying stream zipStream.Close(); } } // show it Logger.WriteEvent("Showing BloomPack on disk"); PathUtilities.SelectFileInExplorer(path); Analytics.Track("Create BloomPack"); } finally { Cursor.Current = Cursors.Default; pleaseWait.Close(); } } } catch (Exception e) { ErrorReport.NotifyUserOfProblem(e, "Could not make the BloomPack at " + path); } }
public void Create() { byte[] correct; using (var temp = new TempFile()) { var writer = File.Create(temp.Path); WriteTestDataToStream(writer); correct = File.ReadAllBytes(temp.Path); } byte[] result; using (var temp = new TempFile()) { var writer = RobustFile.Create(temp.Path); WriteTestDataToStream(writer); result = File.ReadAllBytes(temp.Path); } Assert.That(result, Is.EqualTo(correct)); }
public void AddDirectoryContents_SomeExtensionsExcluded_ExcludedExtensionsSkipped() { // Setup using (var testFolder = new TemporaryFolder(kTestFolderName)) { using (var f1 = RobustFile.Create(Path.Combine(testFolder.Path, "hello.txt"))) { using (var f2 = RobustFile.Create(Path.Combine(testFolder.Path, "temp.tmp"))) { var archive = new MockBloomArchiveSubclass(); var extensionsToExclude = new string[] { ".tmp" }; // System under test archive.AddDirectoryContents(testFolder.Path, extensionsToExclude); // Verification var entryNames = archive.AddFileCallParams.Select(tuple => tuple.Item2); CollectionAssert.AreEquivalent(new string[] { "hello.txt" }, entryNames); } } } }
/// <summary> /// Extract the files in the zip to the specified destination. Then, call filesToDeleteIfNotInZip /// and if any of the files it returned were not in the zip, delete them from the destFolder. /// </summary> public static void ExtractFolderFromZip(string destFolder, string zipPath, Func <HashSet <string> > filesToDeleteIfNotInZip) { byte[] buffer = new byte[4096]; // 4K is optimum var filesInZip = new HashSet <string>(); RetryUtility.Retry(() => { using (var zipFile = new ZipFile(zipPath)) { foreach (ZipEntry entry in zipFile) { filesInZip.Add(entry.Name); var fullOutputPath = Path.Combine(destFolder, entry.Name); var directoryName = Path.GetDirectoryName(fullOutputPath); if (!String.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } using (var instream = zipFile.GetInputStream(entry)) using (var writer = RobustFile.Create(fullOutputPath)) { StreamUtils.Copy(instream, writer, buffer); } } } }); // Remove any sharing-eligible files that are NOT in the zip var filesToDelete = filesToDeleteIfNotInZip(); filesToDelete.ExceptWith(filesInZip); foreach (var discard in filesToDelete) { RobustFile.Delete(Path.Combine(destFolder, discard)); } }
private void _backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { //nb: we want exceptions to be uncaught, to be transferred up to the worker completed event _folderName = GetRootFolderName(); if (_folderName == null) { return; } // ZipFile internally converts all \ separators to / (at least on Linux). So using // ZipFile instead of FastZip fixes https://jira.sil.org/browse/BL-1213. ZipFile zip = null; try { zip = new ZipFile(_path); byte[] buffer = new byte[4096]; // 4K is optimum foreach (ZipEntry entry in zip) { var fullOutputPath = Path.Combine(ProjectContext.GetInstalledCollectionsDirectory(), entry.Name); if (entry.IsDirectory) { Directory.CreateDirectory(fullOutputPath); // In the SharpZipLib code, IsFile and IsDirectory are not defined exactly as inverse: a third // (or fourth) type of entry might be possible. In practice in BloomPacks, this should not be // an issue. continue; } var directoryName = Path.GetDirectoryName(fullOutputPath); if (!String.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } using (var instream = zip.GetInputStream(entry)) using (var writer = RobustFile.Create(fullOutputPath)) { ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(instream, writer, buffer); } } } catch (Exception ex) { // Report a corrupt file instead of crashing. See http://issues.bloomlibrary.org/youtrack/issue/BL-2485. if (InvokeRequired) { Invoke(new ReportBadBloomPack(ReportErrorUnzippingBloomPack)); } else { ReportErrorUnzippingBloomPack(); } return; } finally { if (zip != null) { zip.Close(); } } var newlyAddedFolderOfThePack = Path.Combine(ProjectContext.GetInstalledCollectionsDirectory(), _folderName); CopyXMatterFoldersToWhereTheyBelong(newlyAddedFolderOfThePack); ToolboxView.CopyToolSettingsForBloomPack(newlyAddedFolderOfThePack); }
public BloomTarArchive(string path) { var fsOut = RobustFile.Create(path); _tarStream = new TarOutputStream(fsOut, Encoding.UTF8); }