Can I suggest that you use SharpCompress (http://sharpcompress.codeplex.com/) instead of 7z.dll?
It's native .net code and doesn't rely on an external (non managed) dll, so it can be used with mono.
public static List<FileInfo> ExtractFilesFromZipOrRarFile(string archiveFile)
{
string directory = string.Concat(new object[] { Path.GetTempPath(), Path.DirectorySeparatorChar, Path.GetRandomFileName(), Path.DirectorySeparatorChar });
var archive = ArchiveFactory.Open(archiveFile);
foreach (var entry in archive.Entries)
if (!entry.IsDirectory)
entry.WriteToDirectory(directory, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
List<FileInfo> list = new List<FileInfo>();
foreach (string str2 in Directory.GetFiles(directory))
list.Add(new FileInfo(str2));
return list;
}
I made this as a wrapper for your function from FileUtils:
public static List<FileInfo> ExtractFilesFromZipOrRarFile(string archiveFile)
{
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
var archive = ArchiveFactory.Open(archiveFile);
foreach (var entry in archive.Entries)
{
if (!entry.IsDirectory)
entry.WriteToDirectory(tempDirectory);
}
return Directory.GetFiles(tempDirectory).Select(fileName => new FileInfo(fileName)).ToList();
}
Good point. The archive I tested it on didn't have a directory structure, so I guess I didn't see the issueEDIT: My tests confirm what I said above, ExtractOptions.ExtractFullPath cannot be used. After leaving it out and creating directory manually, everything should work fine.
However I think ExtractOptions.ExtractFullPath should not be there since I believe it would extract files to ...OurTempPath/PathInArchive/foo.file instead of ...OurTempPath/foo.file. In the first case Directory.GetFiles(OurTempPath) would not return anything?
EDIT: My tests confirm what I said above, ExtractOptions.ExtractFullPath cannot be used. After leaving it out and creating directory manually, everything should work fine.
Hello seco,
I think there are legitimate cases where keeping the directory structure could be wanted. In fact, in the sous-titres.eu downloader I wrote, I had to perform some analysis of the structure. Some downloaded zips may contain multiple languages, one per sub directory. Thus, the files are filtered and only those needed are extracted, based on the desired subtitle language. To perform this operation, both file names and directory names are checked. Then, the resulting path is transformed into a more user-friendly file name without a directory structure, which is what is returned to SubtitleDownloader.
This is actually why the sous-titres.eu downloader broke recently. I was using the SevenZip lib directly to have access to the structure for filtering, and as a bonus, only extract the needed files for performance. I now directly use SharpCompress instead. If I could have access to the needed structure without directly referencing the compression library, it could be great. That being said, I realize this case is very specific, and that such a change could easily break compatibility with other downloaders. Currently, I don't really mind using the SharpCompress library: I don't think you'll be changing it again soon, and even if you do, the fix should be quick and easy