From 761d103db2fc6d8b50a55b70b7d72539d4566d04 Mon Sep 17 00:00:00 2001
From: gyk <147011991+gyk4j@users.noreply.github.com>
Date: Tue, 20 Feb 2024 11:59:38 +0800
Subject: [PATCH] Add AttributeView and FileAttributeView interface (#184)
---
JShim/JShim.csproj | 2 +
.../Java/NIO/File/Attribute/AttributeView.cs | 18 +++++
.../File/Attribute/BasicFileAttributeView.cs | 74 ++++++++++++++++++-
.../NIO/File/Attribute/FileAttributeView.cs | 15 ++++
JShim/Java/NIO/File/Files.cs | 4 +-
Wreck/IO/Reader/MetaTag/SevenZipReader.cs | 33 +++++++--
6 files changed, 137 insertions(+), 9 deletions(-)
create mode 100644 JShim/Java/NIO/File/Attribute/AttributeView.cs
create mode 100644 JShim/Java/NIO/File/Attribute/FileAttributeView.cs
diff --git a/JShim/JShim.csproj b/JShim/JShim.csproj
index 7d31946..af3cbc3 100644
--- a/JShim/JShim.csproj
+++ b/JShim/JShim.csproj
@@ -58,7 +58,9 @@
+
+
diff --git a/JShim/Java/NIO/File/Attribute/AttributeView.cs b/JShim/Java/NIO/File/Attribute/AttributeView.cs
new file mode 100644
index 0000000..425dec4
--- /dev/null
+++ b/JShim/Java/NIO/File/Attribute/AttributeView.cs
@@ -0,0 +1,18 @@
+
+using System;
+
+namespace Java.NIO.File.Attribute
+{
+ ///
+ /// 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.
+ ///
+ public interface AttributeView
+ {
+ string Name();
+ }
+}
diff --git a/JShim/Java/NIO/File/Attribute/BasicFileAttributeView.cs b/JShim/Java/NIO/File/Attribute/BasicFileAttributeView.cs
index f5ff22b..63365df 100644
--- a/JShim/Java/NIO/File/Attribute/BasicFileAttributeView.cs
+++ b/JShim/Java/NIO/File/Attribute/BasicFileAttributeView.cs
@@ -6,12 +6,77 @@
namespace Java.NIO.File.Attribute
{
///
- /// 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:
+ ///
+ ///
+ /// Name
+ /// Type
+ ///
+ /// -
+ /// "lastModifiedTime"
+ /// DateTime
+ ///
+ /// -
+ /// "lastAccessTime"
+ /// DateTime
+ ///
+ /// -
+ /// "creationTime"
+ /// DateTime
+ ///
+ /// -
+ /// "size"
+ /// long
+ ///
+ /// -
+ /// "isRegularFile"
+ /// bool
+ ///
+ /// -
+ /// "isDirectory"
+ /// bool
+ ///
+ /// -
+ /// "isSymbolicLink"
+ /// bool
+ ///
+ /// -
+ /// "isOther"
+ /// bool
+ ///
+ /// -
+ /// "fileKey"
+ /// object
+ ///
+ ///
+ ///
+ ///
+ /// The GetAttribute method may be used to read any of these
+ /// attributes as if by invoking the ReadAttributes() method.
+ ///
+ ///
+ ///
+ /// The SetAttribute method may be used to update the file's last
+ /// modified time, last access time or create time attributes as if by
+ /// invoking the SetTimes method.
+ ///
///
- 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)
@@ -70,5 +135,10 @@ private void EnableWriteProtection(bool writeProtect)
fi.IsReadOnly = writeProtect;
}
}
+
+ public string Name()
+ {
+ return Basic;
+ }
}
}
diff --git a/JShim/Java/NIO/File/Attribute/FileAttributeView.cs b/JShim/Java/NIO/File/Attribute/FileAttributeView.cs
new file mode 100644
index 0000000..f4611ea
--- /dev/null
+++ b/JShim/Java/NIO/File/Attribute/FileAttributeView.cs
@@ -0,0 +1,15 @@
+
+using System;
+
+namespace Java.NIO.File.Attribute
+{
+ ///
+ /// 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.
+ ///
+ public interface FileAttributeView : AttributeView
+ {
+ }
+}
diff --git a/JShim/Java/NIO/File/Files.cs b/JShim/Java/NIO/File/Files.cs
index a618a18..56242f3 100644
--- a/JShim/Java/NIO/File/Files.cs
+++ b/JShim/Java/NIO/File/Files.cs
@@ -133,9 +133,9 @@ private static FileVisitResult VisitFile(FileInfo file, FileVisitor visitor)
return result;
}
- public static T GetFileAttributeView(FileSystemInfo fsi, Type type) where T : BasicFileAttributeView
+ public static V GetFileAttributeView(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;
}
}
diff --git a/Wreck/IO/Reader/MetaTag/SevenZipReader.cs b/Wreck/IO/Reader/MetaTag/SevenZipReader.cs
index 011273d..341b8b2 100644
--- a/Wreck/IO/Reader/MetaTag/SevenZipReader.cs
+++ b/Wreck/IO/Reader/MetaTag/SevenZipReader.cs
@@ -83,11 +83,7 @@ public override void Extract(FileSystemInfo file, List 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++)
@@ -120,5 +116,32 @@ public override void Extract(FileSystemInfo file, List 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;
+ }
}
}