|
| 1 | +using System.IO; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | +using ICSharpCode.SharpZipLib.Zip; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace ICSharpCode.SharpZipLib.Tests.Zip |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class FastZipHandling : ZipBase |
| 10 | + { |
| 11 | + [Test] |
| 12 | + [Category("Zip")] |
| 13 | + [Category("CreatesTempFile")] |
| 14 | + public void Basics() |
| 15 | + { |
| 16 | + const string tempName1 = "a(1).dat"; |
| 17 | + |
| 18 | + var target = new MemoryStream(); |
| 19 | + |
| 20 | + string tempFilePath = GetTempFilePath(); |
| 21 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 22 | + |
| 23 | + string addFile = Path.Combine(tempFilePath, tempName1); |
| 24 | + MakeTempFile(addFile, 1); |
| 25 | + |
| 26 | + try { |
| 27 | + var fastZip = new FastZip(); |
| 28 | + fastZip.CreateZip(target, tempFilePath, false, @"a\(1\)\.dat", null); |
| 29 | + |
| 30 | + var archive = new MemoryStream(target.ToArray()); |
| 31 | + using (ZipFile zf = new ZipFile(archive)) { |
| 32 | + Assert.AreEqual(1, zf.Count); |
| 33 | + ZipEntry entry = zf[0]; |
| 34 | + Assert.AreEqual(tempName1, entry.Name); |
| 35 | + Assert.AreEqual(1, entry.Size); |
| 36 | + Assert.IsTrue(zf.TestArchive(true)); |
| 37 | + |
| 38 | + zf.Close(); |
| 39 | + } |
| 40 | + } finally { |
| 41 | + File.Delete(tempName1); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const string ZipTempDir = "SharpZipLibTest"; |
| 46 | + |
| 47 | + void EnsureTestDirectoryIsEmpty(string baseDir) |
| 48 | + { |
| 49 | + string name = Path.Combine(baseDir, ZipTempDir); |
| 50 | + |
| 51 | + if (Directory.Exists(name)) { |
| 52 | + Directory.Delete(name, true); |
| 53 | + } |
| 54 | + |
| 55 | + Directory.CreateDirectory(name); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + [Category("Zip")] |
| 60 | + [Category("CreatesTempFile")] |
| 61 | + public void ExtractEmptyDirectories() |
| 62 | + { |
| 63 | + string tempFilePath = GetTempFilePath(); |
| 64 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 65 | + |
| 66 | + string name = Path.Combine(tempFilePath, "x.zip"); |
| 67 | + |
| 68 | + EnsureTestDirectoryIsEmpty(tempFilePath); |
| 69 | + |
| 70 | + string targetDir = Path.Combine(tempFilePath, ZipTempDir + @"\floyd"); |
| 71 | + using (FileStream fs = File.Create(name)) { |
| 72 | + using (ZipOutputStream zOut = new ZipOutputStream(fs)) { |
| 73 | + zOut.PutNextEntry(new ZipEntry("floyd/")); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + var fastZip = new FastZip(); |
| 78 | + fastZip.CreateEmptyDirectories = true; |
| 79 | + fastZip.ExtractZip(name, targetDir, "zz"); |
| 80 | + |
| 81 | + File.Delete(name); |
| 82 | + Assert.IsTrue(Directory.Exists(targetDir), "Empty directory should be created"); |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + [Category("Zip")] |
| 87 | + public void Encryption() |
| 88 | + { |
| 89 | + const string tempName1 = "a.dat"; |
| 90 | + |
| 91 | + var target = new MemoryStream(); |
| 92 | + |
| 93 | + string tempFilePath = GetTempFilePath(); |
| 94 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 95 | + |
| 96 | + string addFile = Path.Combine(tempFilePath, tempName1); |
| 97 | + MakeTempFile(addFile, 1); |
| 98 | + |
| 99 | + try { |
| 100 | + var fastZip = new FastZip(); |
| 101 | + fastZip.Password = "Ahoy"; |
| 102 | + |
| 103 | + fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); |
| 104 | + |
| 105 | + var archive = new MemoryStream(target.ToArray()); |
| 106 | + using (ZipFile zf = new ZipFile(archive)) { |
| 107 | + zf.Password = "Ahoy"; |
| 108 | + Assert.AreEqual(1, zf.Count); |
| 109 | + ZipEntry entry = zf[0]; |
| 110 | + Assert.AreEqual(tempName1, entry.Name); |
| 111 | + Assert.AreEqual(1, entry.Size); |
| 112 | + Assert.IsTrue(zf.TestArchive(true)); |
| 113 | + Assert.IsTrue(entry.IsCrypted); |
| 114 | + } |
| 115 | + } finally { |
| 116 | + File.Delete(tempName1); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + [Category("Zip")] |
| 122 | + public void CreateExceptions() |
| 123 | + { |
| 124 | + var fastZip = new FastZip(); |
| 125 | + string tempFilePath = GetTempFilePath(); |
| 126 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 127 | + |
| 128 | + Assert.Throws<DirectoryNotFoundException>(() => |
| 129 | + { |
| 130 | + string addFile = Path.Combine(tempFilePath, "test.zip"); |
| 131 | + try { |
| 132 | + fastZip.CreateZip(addFile, @"z:\doesnt exist", false, null); |
| 133 | + } finally { |
| 134 | + File.Delete(addFile); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + [Test] |
| 140 | + [Category("Zip")] |
| 141 | + public void UnicodeText() |
| 142 | + { |
| 143 | + var zippy = new FastZip(); |
| 144 | + var factory = new ZipEntryFactory(); |
| 145 | + factory.IsUnicodeText = true; |
| 146 | + zippy.EntryFactory = factory; |
| 147 | + |
| 148 | + string tempFilePath = GetTempFilePath(); |
| 149 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 150 | + |
| 151 | + const string tempName1 = "a.dat"; |
| 152 | + string addFile = Path.Combine(tempFilePath, tempName1); |
| 153 | + MakeTempFile(addFile, 1); |
| 154 | + |
| 155 | + try { |
| 156 | + var target = new MemoryStream(); |
| 157 | + zippy.CreateZip(target, tempFilePath, false, Regex.Escape(tempName1), null); |
| 158 | + |
| 159 | + var archive = new MemoryStream(target.ToArray()); |
| 160 | + |
| 161 | + using (ZipFile z = new ZipFile(archive)) { |
| 162 | + Assert.AreEqual(1, z.Count); |
| 163 | + Assert.IsTrue(z[0].IsUnicodeText); |
| 164 | + } |
| 165 | + } finally { |
| 166 | + File.Delete(addFile); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + [Test] |
| 171 | + [Category("Zip")] |
| 172 | + public void ExtractExceptions() |
| 173 | + { |
| 174 | + var fastZip = new FastZip(); |
| 175 | + string tempFilePath = GetTempFilePath(); |
| 176 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 177 | + |
| 178 | + string addFile = Path.Combine(tempFilePath, "test.zip"); |
| 179 | + try { |
| 180 | + Assert.Throws<FileNotFoundException>(() => fastZip.ExtractZip(addFile, @"z:\doesnt exist", null)); |
| 181 | + } finally { |
| 182 | + File.Delete(addFile); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + [Test] |
| 187 | + [Category("Zip")] |
| 188 | + [Ignore("see comments below")] |
| 189 | + /* |
| 190 | + * This test is somewhat strange: |
| 191 | + * a) It tries to simulate a locked file by opening it on the same thread using FileShare. |
| 192 | + * However the FileShare value is not meant for cross-process file locking, but only to |
| 193 | + * allow other threads in the same process to access the same file. |
| 194 | + * This is not the intended behavior, you would need a second process locking the file |
| 195 | + * when running this test. |
| 196 | + * b) It would require to change the file operation in FastZip.ProcessFile to use FileShare.ReadWrite |
| 197 | + * but doing so would make FastZip work with locked files (that are potentially written to by others) |
| 198 | + * and silently ignoring any locks. HOWEVER: This can lead to corrupt/incomplete files, which is why it |
| 199 | + * should not be the default behavior. |
| 200 | + * |
| 201 | + * Therefore I would remove this test. |
| 202 | + **/ |
| 203 | + public void ReadingOfLockedDataFiles() |
| 204 | + { |
| 205 | + const string tempName1 = "a.dat"; |
| 206 | + |
| 207 | + var target = new MemoryStream(); |
| 208 | + |
| 209 | + string tempFilePath = GetTempFilePath(); |
| 210 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 211 | + |
| 212 | + string addFile = Path.Combine(tempFilePath, tempName1); |
| 213 | + MakeTempFile(addFile, 1); |
| 214 | + |
| 215 | + try { |
| 216 | + var fastZip = new FastZip(); |
| 217 | + |
| 218 | + using (File.Open(addFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { |
| 219 | + fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); |
| 220 | + |
| 221 | + var archive = new MemoryStream(target.ToArray()); |
| 222 | + using (ZipFile zf = new ZipFile(archive)) { |
| 223 | + Assert.AreEqual(1, zf.Count); |
| 224 | + ZipEntry entry = zf[0]; |
| 225 | + Assert.AreEqual(tempName1, entry.Name); |
| 226 | + Assert.AreEqual(1, entry.Size); |
| 227 | + Assert.IsTrue(zf.TestArchive(true)); |
| 228 | + |
| 229 | + zf.Close(); |
| 230 | + } |
| 231 | + } |
| 232 | + } finally { |
| 233 | + File.Delete(tempName1); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + [Test] |
| 238 | + [Category("Zip")] |
| 239 | + public void NonAsciiPasswords() |
| 240 | + { |
| 241 | + const string tempName1 = "a.dat"; |
| 242 | + |
| 243 | + var target = new MemoryStream(); |
| 244 | + |
| 245 | + string tempFilePath = GetTempFilePath(); |
| 246 | + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); |
| 247 | + |
| 248 | + string addFile = Path.Combine(tempFilePath, tempName1); |
| 249 | + MakeTempFile(addFile, 1); |
| 250 | + |
| 251 | + string password = "abc\u0066\u0393"; |
| 252 | + try { |
| 253 | + var fastZip = new FastZip(); |
| 254 | + fastZip.Password = password; |
| 255 | + |
| 256 | + fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); |
| 257 | + |
| 258 | + var archive = new MemoryStream(target.ToArray()); |
| 259 | + using (ZipFile zf = new ZipFile(archive)) { |
| 260 | + zf.Password = password; |
| 261 | + Assert.AreEqual(1, zf.Count); |
| 262 | + ZipEntry entry = zf[0]; |
| 263 | + Assert.AreEqual(tempName1, entry.Name); |
| 264 | + Assert.AreEqual(1, entry.Size); |
| 265 | + Assert.IsTrue(zf.TestArchive(true)); |
| 266 | + Assert.IsTrue(entry.IsCrypted); |
| 267 | + } |
| 268 | + } finally { |
| 269 | + File.Delete(tempName1); |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | +} |
0 commit comments