Skip to content

Commit

Permalink
Add AttributeView and FileAttributeView interface (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk4j authored Feb 20, 2024
1 parent 4b5f586 commit 761d103
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 9 deletions.
2 changes: 2 additions & 0 deletions JShim/JShim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
<Compile Include="Java\IO\ObjectInputStream.cs" />
<Compile Include="Java\IO\ObjectOutputStream.cs" />
<Compile Include="Java\Lang\Runnable.cs" />
<Compile Include="Java\NIO\File\Attribute\AttributeView.cs" />
<Compile Include="Java\NIO\File\Attribute\BasicFileAttributeView.cs" />
<Compile Include="Java\NIO\File\Attribute\FileAttributeView.cs" />
<Compile Include="Java\Time\Instant.cs" />
<Compile Include="Java\Time\LocalDateTime.cs" />
<Compile Include="Java\Time\ZonedDateTime.cs" />
Expand Down
18 changes: 18 additions & 0 deletions JShim/Java/NIO/File/Attribute/AttributeView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

using System;

namespace Java.NIO.File.Attribute
{
/// <summary>
/// An object that provides a read-only or updatable view of non-opaque
/// values associated with an object in a filesystem. This interface is
/// extended or implemented by specific attribute views that define the
/// attributes supported by the view. A specific attribute view will
/// typically define type-safe methods to read or update the attributes
/// that it supports.
/// </summary>
public interface AttributeView
{
string Name();
}
}
74 changes: 72 additions & 2 deletions JShim/Java/NIO/File/Attribute/BasicFileAttributeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,77 @@
namespace Java.NIO.File.Attribute
{
/// <summary>
/// Description of BasicFileAttributeView.
/// A file attribute view that provides a view of a basic set of file
/// attributes common to many file systems. The basic set of file
/// attributes consist of mandatory and optional file attributes as defined
/// by the BasicFileAttributes interface.
///
/// The file attributes are retrieved from the file system as a bulk
/// operation by invoking the readAttributes method. This class also
/// defines the setTimes method to update the file's time attributes.
///
/// Where dynamic access to file attributes is required, the attributes
/// supported by this attribute view have the following names and types:
/// <list type="table">
/// <listheader>
/// <term>Name</term>
/// <description>Type</description>
/// </listheader>
/// <item>
/// <term>"lastModifiedTime"</term>
/// <description><c>DateTime</c></description>
/// </item>
/// <item>
/// <term>"lastAccessTime"</term>
/// <description><c>DateTime</c></description>
/// </item>
/// <item>
/// <term>"creationTime"</term>
/// <description><c>DateTime</c></description>
/// </item>
/// <item>
/// <term>"size"</term>
/// <description><c>long</c></description>
/// </item>
/// <item>
/// <term>"isRegularFile"</term>
/// <description><c>bool</c></description>
/// </item>
/// <item>
/// <term>"isDirectory"</term>
/// <description><c>bool</c></description>
/// </item>
/// <item>
/// <term>"isSymbolicLink"</term>
/// <description><c>bool</c></description>
/// </item>
/// <item>
/// <term>"isOther"</term>
/// <description><c>bool</c></description>
/// </item>
/// <item>
/// <term>"fileKey"</term>
/// <description><c>object</c></description>
/// </item>
/// </list>
///
/// <para>
/// The <c>GetAttribute</c> method may be used to read any of these
/// attributes as if by invoking the <c>ReadAttributes()</c> method.
/// </para>
///
/// <para>
/// The <c>SetAttribute</c> method may be used to update the file's last
/// modified time, last access time or create time attributes as if by
/// invoking the <c>SetTimes</c> method.
/// </para>
/// </summary>
public class BasicFileAttributeView
public class BasicFileAttributeView : FileAttributeView
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(BasicFileAttributeView));

public const string Basic = "basic";

private FileSystemInfo file;

public BasicFileAttributeView(FileSystemInfo file)
Expand Down Expand Up @@ -70,5 +135,10 @@ private void EnableWriteProtection(bool writeProtect)
fi.IsReadOnly = writeProtect;
}
}

public string Name()
{
return Basic;
}
}
}
15 changes: 15 additions & 0 deletions JShim/Java/NIO/File/Attribute/FileAttributeView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using System;

namespace Java.NIO.File.Attribute
{
/// <summary>
/// An attribute view that is a read-only or updatable view of non-opaque
/// values associated with a file in a filesystem. This interface is
/// extended or implemented by specific file attribute views that define
/// methods to read and/or update the attributes of a file.
/// </summary>
public interface FileAttributeView : AttributeView
{
}
}
4 changes: 2 additions & 2 deletions JShim/Java/NIO/File/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ private static FileVisitResult VisitFile(FileInfo file, FileVisitor visitor)
return result;
}

public static T GetFileAttributeView<T>(FileSystemInfo fsi, Type type) where T : BasicFileAttributeView
public static V GetFileAttributeView<V>(FileSystemInfo fsi, Type type) where V : FileAttributeView
{
T view = (T) Activator.CreateInstance(type, new object[] { fsi });
V view = (V) Activator.CreateInstance(type, new object[] { fsi });
return view;
}
}
Expand Down
33 changes: 28 additions & 5 deletions Wreck/IO/Reader/MetaTag/SevenZipReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public override void Extract(FileSystemInfo file, List<Metadata> metadata)

try
{
string password = passwordProvider.GetPassword(file);

using(SevenZipExtractor sze = string.IsNullOrEmpty(password)?
new SevenZipExtractor(file.FullName):
new SevenZipExtractor(file.FullName, password))
using(SevenZipExtractor sze = Get7ZipExtractor((FileInfo) file))
{
DateTime modified = DateTime.MinValue;
for (int i = 0; i < sze.ArchiveFileData.Count; i++)
Expand Down Expand Up @@ -120,5 +116,32 @@ public override void Extract(FileSystemInfo file, List<Metadata> metadata)
// throw new ArgumentException(ex.Message, ex);
}
}

private SevenZipExtractor Get7ZipExtractor(FileInfo file)
{
SevenZipExtractor sze;

string password = passwordProvider.GetPassword(file);

// HACK: Specify archive format for unrecognized extensions.
if(file.Extension.EndsWith(".cbr"))
{
// Format is specified. Tell 7-Zip to treat file as certain format.
InArchiveFormat rar = InArchiveFormat.Rar;

sze = string.IsNullOrEmpty(password)?
new SevenZipExtractor(file.FullName, rar):
new SevenZipExtractor(file.FullName, password, rar);
}
else
{
// If no format is specified, use auto-detection by 7-Zip library.
sze = string.IsNullOrEmpty(password)?
new SevenZipExtractor(file.FullName):
new SevenZipExtractor(file.FullName, password);
}

return sze;
}
}
}

0 comments on commit 761d103

Please sign in to comment.