예제 #1
0
            public ZipEntryStream(FileShare share, ZipArchiveFileSystem system, ZipArchiveEntry entry)
            {
                _entry      = entry;
                _fileSystem = system;

                lock (_fileSystem._openStreamsLock)
                {
                    var fileShare = _fileSystem._openStreams.TryGetValue(entry, out var fileData) ? fileData.Share : FileShare.ReadWrite;
                    if (fileData != null)
                    {
                        // we only check for read share, because ZipArchive doesn't support write share
                        if (share is not FileShare.Read and not FileShare.ReadWrite)
                        {
                            throw new IOException("File is already opened for reading");
                        }

                        if (fileShare is not FileShare.Read and not FileShare.ReadWrite)
                        {
                            throw new IOException("File is already opened for reading by another stream with non compatible share");
                        }

                        fileData.Count++;
                    }
                    else
                    {
                        _fileSystem._openStreams.Add(_entry, new EntryState(share));
                    }
                    _streamImplementation = entry.Open();
                }

                Share = share;
            }
예제 #2
0
        public void TestOpenStreamsMultithreaded()
        {
            var memStream = new MemoryStream();
            var writeFs   = new ZipArchiveFileSystem(memStream, ZipArchiveMode.Update, true);

            writeFs.WriteAllText("/test.txt", "content");
            writeFs.Dispose();

            var readFs = new ZipArchiveFileSystem(memStream, ZipArchiveMode.Read);

            readFs.OpenFile("/test.txt", FileMode.Open, FileAccess.Read, FileShare.Read).Dispose();

            const int CountTest = 2000;

            var thread1 = new Thread(() =>
            {
                for (int i = 0; i < CountTest; i++)
                {
                    readFs.OpenFile("/test.txt", FileMode.Open, FileAccess.Read, FileShare.Read).Dispose();
                }
            });
            var thread2 = new Thread(() =>
            {
                for (int i = 0; i < CountTest; i++)
                {
                    readFs.OpenFile("/test.txt", FileMode.Open, FileAccess.Read, FileShare.Read).Dispose();
                }
            });

            thread1.Start();
            thread2.Start();

            thread1.Join();
            thread2.Join();
        }
예제 #3
0
        protected ZipArchiveFileSystem GetCommonZipArchiveFileSystem()
        {
            var archive = new ZipArchive(new MemoryStream(), ZipArchiveMode.Update);
            var fs      = new ZipArchiveFileSystem(archive);

            CreateFolderStructure(fs);
            return(fs);
        }
예제 #4
0
        public void TestCopyFileSystem()
        {
            var fs = this.GetCommonZipArchiveFileSystem();

            var dest = new ZipArchiveFileSystem(new MemoryStream());

            fs.CopyTo(dest, UPath.Root, true);

            this.AssertFileSystemEqual(fs, dest);
        }
예제 #5
0
        public void TestGetParentDirectory()
        {
            var fs        = new ZipArchiveFileSystem();
            var fileEntry = new FileEntry(fs, "/test/tata/titi.txt");
            // Shoud not throw an error
            var directory = fileEntry.Directory;

            Assert.False(directory.Exists);
            Assert.Equal(UPath.Root / "test/tata", directory.Path);
        }
예제 #6
0
        public void TestCopyFileSystemSubFolder()
        {
            var fs = GetCommonZipArchiveFileSystem();

            var dest      = new ZipArchiveFileSystem(new MemoryStream());
            var subFolder = UPath.Root / "subfolder";

            fs.CopyTo(dest, subFolder, true);

            var destSubFileSystem = dest.GetOrCreateSubFileSystem(subFolder);

            AssertFileSystemEqual(fs, destSubFileSystem);
        }
예제 #7
0
        public void TestOpenFileMultipleRead()
        {
            var memStream   = new MemoryStream();
            var writeSystem = new ZipArchiveFileSystem(memStream, ZipArchiveMode.Update, true);

            writeSystem.WriteAllText("/toto.txt", "content");
            writeSystem.Dispose();

            var readSystem = new ZipArchiveFileSystem(memStream, ZipArchiveMode.Read, true);

            Assert.True(readSystem.FileExists("/toto.txt"));


            using (var tmp = readSystem.OpenFile("/toto.txt", FileMode.Open, FileAccess.Read))
            {
                Assert.Throws <IOException>(() => readSystem.OpenFile("/toto.txt", FileMode.Open, FileAccess.Read, FileShare.Read));
            }

            var stream1 = readSystem.OpenFile("/toto.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            var stream2 = readSystem.OpenFile("/toto.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

            // DeflateStream used by ZipArchive in read mode does not support Position and Length
            //Assert.Equal<long>(1, stream1.Position);
            Assert.Equal <char>('c', (char)stream1.ReadByte());

            Assert.Equal <char>('c', (char)stream2.ReadByte());
            //Assert.Equal<long>(2, stream2.Position);

            stream1.Dispose();
            stream2.Dispose();

            readSystem.Dispose();

            // We try to write back on the same file after closing
            writeSystem = new ZipArchiveFileSystem(memStream, ZipArchiveMode.Update);
            writeSystem.WriteAllText("/toto.txt", "content2");
        }
예제 #8
0
 public TestZipArchiveFileSystemCompact()
 {
     fs = new ZipArchiveFileSystem();
 }