public void RoundTrip() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { try { MakeZipFile(tempFile, "", 10, 1024, ""); using (IsolatedZipFile zipFile = new IsolatedZipFile(tempFile)) { foreach (ZipEntry e in zipFile) { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1024); } zipFile.Close(); } } finally { store.DeleteFile(tempFile); } } }
public void CreateEmptyArchive() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (IsolatedZipFile f = IsolatedZipFile.Create(tempFile)) { f.BeginUpdate(); f.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true)); f.Close(); } using (IsolatedZipFile f = new IsolatedZipFile(tempFile)) { Assert.AreEqual(0, f.Count); } store.DeleteFile(tempFile); } }
public void UnicodeText() { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { IsolatedFastZip zippy = new IsolatedFastZip(); ZipEntryFactory factory = new ZipEntryFactory(); factory.IsUnicodeText = true; zippy.EntryFactory = factory; string tempFilePath = GetTempFilePath(); Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); const string tempName1 = "a.dat"; string addFile = Path.Combine(tempFilePath, tempName1); MakeTempFile(addFile, 1); try { MemoryStream target = new MemoryStream(); zippy.CreateZip(target, tempFilePath, false, tempName1, null); // failing here in 4 MemoryStream archive = new MemoryStream(target.ToArray()); using (IsolatedZipFile z = new IsolatedZipFile(archive)) { Assert.AreEqual(1, z.Count); Assert.IsTrue(z[0].IsUnicodeText); } } finally { store.DeleteFile(addFile); } } }
public void Zip64Entries() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); const int target = 65537; using (IsolatedZipFile zipFile = IsolatedZipFile.Create(Path.GetTempFileName())) { zipFile.BeginUpdate(); for (int i = 0; i < target; ++i) { ZipEntry ze = new ZipEntry(i.ToString()); ze.CompressedSize = 0; ze.Size = 0; zipFile.Add(ze); } zipFile.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(zipFile.TestArchive(true)); Assert.AreEqual(target, zipFile.Count, "Incorrect number of entries stored"); } }
/// <summary> /// </summary> private void ZipFileToISO(object sender, RoutedEventArgs e) { DeleteSampleText(); DeleteSampleZip(); CreateLocalSampleFile(SampleTextFileName); /* * Create an archive with ZipFile ************************************************************* */ IsolatedZipFile zip = IsolatedZipFile.Create(SampleZipFileName); zip.BeginUpdate(); zip.Add(SampleTextFileName); zip.CommitUpdate(); zip.Close(); /************************************************************* * */ DisplayMetrics("CreateIsoZipFile", false); DeleteSampleText(); UnzipFromISOButton.IsEnabled = true; ZipFileFromISOButton.IsEnabled = true; ZipStreamFromISOButton.IsEnabled = false; }
/// <summary> /// </summary> private void ZipFileFromISO(object sender, RoutedEventArgs e) { DeleteSampleText(); /* * Extract an archive using IsolatedZipFile ************************************************************* */ using (var zip = new IsolatedZipFile(SampleZipFileName)) { var entry = zip.GetEntry(SampleTextFileName); using (var streamIn = zip.GetInputStream(entry)) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var streamOut = store.OpenFile(SampleTextFileName, FileMode.Create)) { streamIn.CopyTo(streamOut); } } } } /************************************************************* * */ DisplayMetrics("ZipFileFromISO", false); }
public void FindEntriesInArchiveWithLongComment() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); string longComment = new String('A', 65535); MakeZipFile(tempFile, "", 1, 1, longComment); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { try { using (IsolatedZipFile zipFile = new IsolatedZipFile(tempFile)) { foreach (ZipEntry e in zipFile) { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1); } zipFile.Close(); } } finally { store.DeleteFile(tempFile); } } }
public void Noid() { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream fs = store.CreateFile("foo.txt")) { using (var writer = new StreamWriter(fs)) { writer.Write("Foo Bar FU!"); } } store.CreateDirectory("foobarfu"); string fooText = Guid.NewGuid().ToString(); using (var createdFile = store.CreateFile("foo.txt")) { using (var writer = new StreamWriter(createdFile)) { writer.Write(fooText); } } using (var createdFile = store.CreateFile("foobarfu\\foo.txt")) { using (var writer = new StreamWriter(createdFile)) { writer.Write(fooText); } } var factory = new IsolatedZipEntryFactory(); ZipEntry fentry = factory.MakeFileEntry("foo.txt"); ZipEntry dentry = factory.MakeDirectoryEntry("foobarfu"); using (var f = IsolatedZipFile.Create("foo.zip")) { f.BeginUpdate(); f.Add("foo.txt"); f.CommitUpdate(); } using (var f = new IsolatedZipFile("foo.zip")) { var entry = f.GetEntry("foo.txt"); string content = new StreamReader(f.GetInputStream(entry)).ReadToEnd(); Assert.AreEqual(fooText, content); } var fz = new IsolatedFastZip(); fz.CreateZip("fz.zip", "foobarfu", true, ""); } }
/// <summary> /// Initializes a new instance of the <see cref = "DiskArchiveStorage" /> class. /// </summary> /// <param name = "file">The file.</param> /// <param name = "updateMode">The update mode.</param> public IsolatedDiskArchiveStorage(IsolatedZipFile file, FileUpdateMode updateMode) : base(updateMode) { if (file.Name == null) { throw new ZipException("Cant handle non file archives"); } fileName_ = file.Name; }
/// <summary> /// Extract the contents of a zip file held in a stream. /// </summary> /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param> /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param> public void ExtractZip(Stream inputStream, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime, bool isStreamOwner) { if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) { throw new ArgumentNullException("confirmDelegate"); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; extractNameTransform_ = new IsolatedNameTransform(targetDirectory); fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; using (zipFile_ = new IsolatedZipFile(inputStream)) { if (password_ != null) { zipFile_.Password = password_; } zipFile_.IsStreamOwner = isStreamOwner; System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator(); while (continueRunning_ && enumerator.MoveNext()) { ZipEntry entry = (ZipEntry)enumerator.Current; if (entry.IsFile) { // TODO Path.GetDirectory can fail here on invalid characters. if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) { ExtractEntry(entry); } } else if (entry.IsDirectory) { if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) { ExtractEntry(entry); } } else { // Do nothing for volume labels etc... } } } }
public void FindEntry() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); MakeZipFile(tempFile, new string[] { "Farriera", "Champagne", "Urban myth" }, 10, "Aha"); using (IsolatedZipFile zipFile = new IsolatedZipFile(tempFile)) { Assert.AreEqual(3, zipFile.Count, "Expected 1 entry"); int testIndex = zipFile.FindEntry("Farriera", false); Assert.AreEqual(0, testIndex, "Case sensitive find failure"); Assert.IsTrue(string.Compare(zipFile[testIndex].Name, "Farriera", StringComparison.InvariantCulture) == 0); testIndex = zipFile.FindEntry("farriera", false); Assert.AreEqual(-1, testIndex, "Case sensitive find failure"); testIndex = zipFile.FindEntry("farriera", true); Assert.AreEqual(0, testIndex, "Case insensitive find failure"); Assert.IsTrue(string.Compare(zipFile[testIndex].Name, "Farriera", StringComparison.InvariantCultureIgnoreCase) == 0); testIndex = zipFile.FindEntry("urban mYTH", false); Assert.AreEqual(-1, testIndex, "Case sensitive find failure"); testIndex = zipFile.FindEntry("urban mYTH", true); Assert.AreEqual(2, testIndex, "Case insensitive find failure"); Assert.IsTrue(string.Compare(zipFile[testIndex].Name, "urban mYTH", StringComparison.InvariantCultureIgnoreCase) == 0); testIndex = zipFile.FindEntry("Champane.", false); Assert.AreEqual(-1, testIndex, "Case sensitive find failure"); testIndex = zipFile.FindEntry("Champane.", true); Assert.AreEqual(-1, testIndex, "Case insensitive find failure"); zipFile.Close(); } store.DeleteFile(tempFile); } }
public void AddToEmptyArchive() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); string addFile = Path.Combine(tempFile, "a.dat"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { MakeTempFile(addFile, 1); try { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (IsolatedZipFile f = IsolatedZipFile.Create(tempFile)) { f.BeginUpdate(); f.Add(addFile); f.CommitUpdate(); Assert.AreEqual(1, f.Count); // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true)); } using (IsolatedZipFile f = new IsolatedZipFile(tempFile)) { Assert.AreEqual(1, f.Count); f.BeginUpdate(); f.Delete(f[0]); // failing here in 4 and 3 - fixed - one-off in zipfile f.CommitUpdate(); Assert.AreEqual(0, f.Count); // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true)); f.Close(); } store.DeleteFile(tempFile); } finally { store.DeleteFile(addFile); } } }
public void BasicEncryptionToDisk() { const string TestValue = "0001000"; string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedZipFile f = IsolatedZipFile.Create(tempFile)) { f.Password = "******"; StringMemoryDataSource m = new StringMemoryDataSource(TestValue); f.BeginUpdate(); f.Add(m, "a.dat"); f.CommitUpdate(); } using (IsolatedZipFile f = new IsolatedZipFile(tempFile)) { f.Password = "******"; // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true), "Archive test should pass"); } using (IsolatedZipFile g = new IsolatedZipFile(tempFile)) { g.Password = "******"; ZipEntry ze = g[0]; Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted"); using (StreamReader r = new StreamReader(g.GetInputStream(0))) { string data = r.ReadToEnd(); Assert.AreEqual(TestValue, data); } } store.DeleteFile(tempFile); } }
public void HandlesNoEntries() { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); MakeZipFile(tempFile, "", 0, 1, "Aha"); using (IsolatedZipFile zipFile = new IsolatedZipFile(tempFile)) { Assert.AreEqual(0, zipFile.Count); zipFile.Close(); } store.DeleteFile(tempFile); } }
public void NonAsciiPasswords() { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { const string tempName1 = "a.dat"; MemoryStream target = new MemoryStream(); string tempFilePath = GetTempFilePath(); Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); string addFile = Path.Combine(tempFilePath, tempName1); MakeTempFile(addFile, 1); string password = "******"; try { IsolatedFastZip fastZip = new IsolatedFastZip(); fastZip.Password = password; fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); // failing here in 4 MemoryStream archive = new MemoryStream(target.ToArray()); using (IsolatedZipFile zf = new IsolatedZipFile(archive)) { zf.Password = password; Assert.AreEqual(1, zf.Count); ZipEntry entry = zf[0]; Assert.AreEqual(tempName1, entry.Name); Assert.AreEqual(1, entry.Size); // TODO: Testing is not yet ported - Assert.IsTrue(zf.TestArchive(true)); Assert.IsTrue(entry.IsCrypted); } } finally { store.DeleteFile(tempName1); } } }
public void AddAndDeleteEntries() { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); string addFile = Path.Combine(tempFile, "a.dat"); MakeTempFile(addFile, 1); string addFile2 = Path.Combine(tempFile, "b.dat"); MakeTempFile(addFile2, 259); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (IsolatedZipFile f = IsolatedZipFile.Create(tempFile)) { f.BeginUpdate(); f.Add(addFile); f.Add(addFile2); f.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true)); } using (IsolatedZipFile f = new IsolatedZipFile(tempFile)) { Assert.AreEqual(2, f.Count); // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true)); f.BeginUpdate(); f.Delete(f[0]); f.CommitUpdate(); Assert.AreEqual(1, f.Count); // TODO: Testing is not yet ported - Assert.IsTrue(f.TestArchive(true)); } store.DeleteFile(addFile); store.DeleteFile(addFile2); store.DeleteFile(tempFile); } }
public void FindEntriesInArchiveExtraData() { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); bool fails; using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); string longComment = new String('A', 65535); FileStream tempStream = store.CreateFile(tempFile); MakeZipFile(tempStream, false, "", 1, 1, longComment); tempStream.WriteByte(85); tempStream.Close(); fails = false; try { using (IsolatedZipFile zipFile = new IsolatedZipFile(tempFile)) { foreach (ZipEntry e in zipFile) { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1); } zipFile.Close(); } } catch { fails = true; } store.DeleteFile(tempFile); } Assert.IsTrue(fails, "Currently zip file wont be found"); }
public void PartialStreamClosing() { string tempFile = GetTempFilePath(); if (tempFile != null) { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); MakeZipFile(tempFile, new String[] { "Farriera", "Champagne", "Urban myth" }, 10, "Aha"); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedZipFile zipFile = new IsolatedZipFile(tempFile)) { Stream stream = zipFile.GetInputStream(0); stream.Close(); stream = zipFile.GetInputStream(1); zipFile.Close(); } store.DeleteFile(tempFile); } } }
/// <summary> /// Initializes a new instance of the <see cref = "DiskArchiveStorage" /> class. /// </summary> /// <param name = "file">The file.</param> public IsolatedDiskArchiveStorage(IsolatedZipFile file) : this(file, FileUpdateMode.Safe) { }
public void UpdateCommentOnlyOnDisk() { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); if (store.FileExists(tempFile)) { store.DeleteFile(tempFile); } using (IsolatedZipFile testFile = IsolatedZipFile.Create(tempFile)) { testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); } using (IsolatedZipFile testFile = new IsolatedZipFile(tempFile)) { // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); Assert.AreEqual("", testFile.ZipFileComment); testFile.BeginUpdate(new IsolatedDiskArchiveStorage(testFile, FileUpdateMode.Direct)); testFile.SetComment("Here is my comment"); testFile.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); } using (IsolatedZipFile testFile = new IsolatedZipFile(tempFile)) { // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); Assert.AreEqual("Here is my comment", testFile.ZipFileComment); } store.DeleteFile(tempFile); // Variant using indirect updating. using (IsolatedZipFile testFile = IsolatedZipFile.Create(tempFile)) { testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); } using (IsolatedZipFile testFile = new IsolatedZipFile(tempFile)) { // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); Assert.AreEqual("", testFile.ZipFileComment); testFile.BeginUpdate(); testFile.SetComment("Here is my comment"); testFile.CommitUpdate(); // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); } using (IsolatedZipFile testFile = new IsolatedZipFile(tempFile)) { // TODO: Testing is not yet ported - Assert.IsTrue(testFile.TestArchive(true)); Assert.AreEqual("Here is my comment", testFile.ZipFileComment); } store.DeleteFile(tempFile); store.DeleteDirectory(Path.GetDirectoryName(tempFile)); } }
/// <summary> /// Extract the contents of a zip file held in a stream. /// </summary> /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param> /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param> public void ExtractZip(Stream inputStream, string targetDirectory,Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate,string fileFilter, string directoryFilter, bool restoreDateTime,bool isStreamOwner) { if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) { throw new ArgumentNullException("confirmDelegate"); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; extractNameTransform_ = new IsolatedNameTransform(targetDirectory); fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; using (zipFile_ = new IsolatedZipFile(inputStream)) { if (password_ != null) { zipFile_.Password = password_; } zipFile_.IsStreamOwner = isStreamOwner; System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator(); while (continueRunning_ && enumerator.MoveNext()) { ZipEntry entry = (ZipEntry)enumerator.Current; if (entry.IsFile) { // TODO Path.GetDirectory can fail here on invalid characters. if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) { ExtractEntry(entry); } } else if (entry.IsDirectory) { if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) { ExtractEntry(entry); } } else { // Do nothing for volume labels etc... } } } }