Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a file system based on the .NET ZipArchive (available as of .ne… #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions SharpFileSystem.Tests/NetZipArchive/NetZipArchiveFileSystemTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;
using SharpFileSystem.IO;
using SharpFileSystem.SharpZipArchive;

namespace SharpFileSystem.Tests.NetZipArchive
{
[TestFixture]
public class NetZipArchiveFileSystemTest
{
private Stream zipStream;
private NetZipArchiveFileSystem fileSystem;
private string fileContentString = "this is a file";
[OneTimeSetUp]
public void Initialize()
{
var memoryStream = new MemoryStream();
zipStream = memoryStream;
var zipOutput = new ZipOutputStream(zipStream);


var fileContentBytes = Encoding.ASCII.GetBytes(fileContentString);
zipOutput.PutNextEntry(new ZipEntry("textfileA.txt")
{
Size = fileContentBytes.Length
});
zipOutput.Write(fileContentBytes);
zipOutput.PutNextEntry(new ZipEntry("directory/fileInDirectory.txt"));
zipOutput.PutNextEntry(new ZipEntry("scratchdirectory/scratch"));
zipOutput.Finish();

memoryStream.Position = 0;
fileSystem = NetZipArchiveFileSystem.Open(zipStream);
}

[OneTimeTearDown]
public void Cleanup()
{
fileSystem.Dispose();
zipStream.Dispose();
}

private readonly FileSystemPath directoryPath = FileSystemPath.Parse("/directory/");
private readonly FileSystemPath textfileAPath = FileSystemPath.Parse("/textfileA.txt");
private readonly FileSystemPath fileInDirectoryPath = FileSystemPath.Parse("/directory/fileInDirectory.txt");
private readonly FileSystemPath scratchDirectoryPath = FileSystemPath.Parse("/scratchdirectory/");

[Test]
public void GetEntitiesOfRootTest()
{
CollectionAssert.AreEquivalent(new[]
{
textfileAPath,
directoryPath,
scratchDirectoryPath
}, fileSystem.GetEntities(FileSystemPath.Root).ToArray());
}

[Test]
public void GetEntitiesOfDirectoryTest()
{
CollectionAssert.AreEquivalent(new[]
{
fileInDirectoryPath
}, fileSystem.GetEntities(directoryPath).ToArray());
}

[Test]
public void ExistsTest()
{
Assert.IsTrue(fileSystem.Exists(FileSystemPath.Root));
Assert.IsTrue(fileSystem.Exists(textfileAPath));
Assert.IsTrue(fileSystem.Exists(directoryPath));
Assert.IsTrue(fileSystem.Exists(fileInDirectoryPath));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingFile")));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingDirectory/")));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/directory/nonExistingFileInDirectory")));
}

[Test]
public void CanReadFile()
{
var file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.IsTrue(string.Equals(text, fileContentString));
}

[Test]
public void CanWriteFile()
{
var file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var textBytes = Encoding.ASCII.GetBytes(fileContentString + " and a new string");
file.Write(textBytes);
file.Close();


file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.IsTrue(string.Equals(text, fileContentString + " and a new string"));
}

[Test]
public void CanAddFile()
{
var fsp = FileSystemPath.Parse("/scratchdirectory/recentlyadded.txt");
var file = fileSystem.CreateFile(fsp);
var textBytes = Encoding.ASCII.GetBytes("recently added");
file.Write(textBytes);
file.Close();

Assert.IsTrue(fileSystem.Exists(fsp));

file = fileSystem.OpenFile(fsp, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.IsTrue(string.Equals(text, "recently added"));
}

[Test]
public void CanAddDirectory()
{
var fsp = FileSystemPath.Parse("/scratchdirectory/newdir/");
fileSystem.CreateDirectory(fsp);

Assert.IsTrue(fileSystem.Exists(fsp));
}
}
}
9 changes: 7 additions & 2 deletions SharpFileSystem.Tests/SharpFileSystem.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -43,12 +43,17 @@
<Compile Include="FileSystems\EntityMoverRegistrationTest.cs" />
<Compile Include="FileSystems\MemoryFileSystemTest.cs" />
<Compile Include="FileSystems\PhysicalFileSystemTest.cs" />
<Compile Include="NetZipArchive\NetZipArchiveFileSystemTest.cs" />
<Compile Include="SharpZipLib\SharpZipLibFileSystemTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpFileSystem.ZipArchive\SharpFileSystem.NetZipArchive.csproj">
<Project>{8d44f07f-a1f8-4544-8eef-b2fd99d0fba2}</Project>
<Name>SharpFileSystem.NetZipArchive</Name>
</ProjectReference>
<ProjectReference Include="..\SharpFileSystem\SharpFileSystem.csproj">
<Project>{3C4D07BA-23B5-4A63-92CF-F3BF892B7329}</Project>
<Name>SharpFileSystem</Name>
Expand All @@ -72,4 +77,4 @@
</Content>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
103 changes: 103 additions & 0 deletions SharpFileSystem.ZipArchive/NetZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;

namespace SharpFileSystem.SharpZipArchive
{
public class NetZipArchiveFileSystem : IFileSystem
{
public ZipArchive ZipArchive { get; private set; }

public static NetZipArchiveFileSystem Open(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s,ZipArchiveMode.Update,true));
}

public static NetZipArchiveFileSystem Create(Stream s)
{
return new NetZipArchiveFileSystem(new ZipArchive(s, ZipArchiveMode.Create, true));
}

private NetZipArchiveFileSystem(ZipArchive archive)
{
ZipArchive = archive;
}
public void Dispose()
{
ZipArchive.Dispose();
}

protected IEnumerable<ZipArchiveEntry> GetZipEntries()
{
return ZipArchive.Entries;
}
protected FileSystemPath ToPath(ZipArchiveEntry entry)
{
return FileSystemPath.Parse(FileSystemPath.DirectorySeparator + entry.FullName);
}
protected string ToEntryPath(FileSystemPath path)
{
// Remove heading '/' from path.
return path.Path.TrimStart(FileSystemPath.DirectorySeparator);
}

protected ZipArchiveEntry ToEntry(FileSystemPath path)
{
return ZipArchive.GetEntry(ToEntryPath(path));
}
public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
return GetZipEntries().Select(ToPath).Where(path.IsParentOf)
.Select(entryPath => entryPath.ParentPath == path
? entryPath
: path.AppendDirectory(entryPath.RemoveParent(path).GetDirectorySegments()[0])
)
.Distinct()
.ToList();
}

public bool Exists(FileSystemPath path)
{
if (path.IsFile)
return ToEntry(path) != null;
return GetZipEntries()
.Select(ToPath)
.Any(entryPath => entryPath.IsChildOf(path));

//foreach (var zipArchiveEntry in GetZipEntries())
//{
// var p = ToPath(zipArchiveEntry);
// if(p.IsChildOf(path) )
// return true;
//}
//return false;
}

public Stream CreateFile(FileSystemPath path)
{
var zae = ZipArchive.CreateEntry(ToEntryPath(path));
return zae.Open();
}

public Stream OpenFile(FileSystemPath path, FileAccess access)
{
var zae = ZipArchive.GetEntry(ToEntryPath(path));
return zae.Open();
}

public void CreateDirectory(FileSystemPath path)
{
ZipArchive.CreateEntry(ToEntryPath(path));
}

public void Delete(FileSystemPath path)
{
var zae = ZipArchive.GetEntry(ToEntryPath(path));
zae.Delete();
}
}
}
36 changes: 36 additions & 0 deletions SharpFileSystem.ZipArchive/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SharpFileSystem.ZipArchive")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SharpFileSystem.ZipArchive")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8d44f07f-a1f8-4544-8eef-b2fd99d0fba2")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
56 changes: 56 additions & 0 deletions SharpFileSystem.ZipArchive/SharpFileSystem.NetZipArchive.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8D44F07F-A1F8-4544-8EEF-B2FD99D0FBA2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SharpFileSystem.SharpZipArchive</RootNamespace>
<AssemblyName>SharpFileSystem.SharpZipArchive</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="NetZipArchiveFileSystem.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SharpFileSystem\SharpFileSystem.csproj">
<Project>{3C4D07BA-23B5-4A63-92CF-F3BF892B7329}</Project>
<Name>SharpFileSystem</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading