2016-05-29 11 views
1

ich automatisch einen ganzen Ordner von meiner Dropbox Download - Lagerung, über den Weblink zB (?): https://www.dropbox.com/sh/bunchOfLetters/somthnsomthn?dl=1 mit nur einem Grund webclient:Dropbox-Ordner herunterladen Ausgabe (C#)

using (WebClient wc = new WebClient()) 
{ 
    wc.DownloadFile(new Uri(uri), _basePath + localPath); 
} 

Der Ordner naturaly kommt als .rar-Datei (aufgrund des Dropbox-Download-Systems), und wenn ich versuche, die Datei manuell (über Winrar) zu öffnen, gibt es überhaupt kein Problem. Jetzt kommt das Problem .... wenn ich versuche, irgendeine Art von automatisierter Bibliothek (wie unrar oder SharpCompress) zu benutzen, gibt es immer einen 'Corupted Header', als ob der Download nicht alle bekommen hätte oder die Datei gerade kaputt gegangen wäre ... aber trotzdem Öffnen mit Winrar funktioniert gut.

Wenn jemand eine Idee wie und warum hat; Ich wäre großartig, deine Gedanken zu hören.

Edit:

hier ist die Funktion i für SharpCompress verwenden:

public static bool ExtractToFolder(string extractionPackage, string outputPath) 
     { 
      if (extractionPackage == null) throw new ArgumentNullException(nameof(extractionPackage)); 
      if (outputPath == null) throw new ArgumentNullException(nameof(outputPath)); 
      if (string.IsNullOrEmpty(extractionPackage)) 
       throw new ArgumentException("Argument is null or empty", nameof(extractionPackage)); 
      if (string.IsNullOrEmpty(outputPath)) 
       throw new ArgumentException("Argument is null or empty", nameof(outputPath)); 
      if (!extractionPackage.EndsWith(".rar")) throw new InvalidDataException("not a .rar File"); 
      Directory.CreateDirectory(outputPath); 

      try 
      { 
       using (Stream stream = File.OpenRead(extractionPackage)) 
       using (RarReader reader = RarReader.Open(stream)) 
       { 
        while (reader.MoveToNextEntry()) 
        { 
         reader.WriteEntryToDirectory(outputPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); 
        } 
       } 

       return true; 
      } 
      catch 
      { 
       return false; 
      } 
     } 

und wenn jemand interessiert sich für die unrar ein: ..i wissen es nicht gut gemacht, aber es immer noch funktioniert für normale Dateien ... nur nicht Dropbox diejenigen

namespace Cryptus.Rar 
{ 
/// <summary> 
/// just for compatibility purposes, use <see cref="SharpCompress"/> 
/// </summary> 
public class Unrar 
{ 
    public enum RarOperations 
    { 
     OP_EXTRACT = 0, 
     OP_TEST = 1, 
     OP_LIST = 2 
    } 

    public const int ERAR_END_ARCHIVE = 10; 
    public const int ERAR_NO_MEMORY = 11; 
    public const int ERAR_BAD_DATA = 12; 
    public const int ERAR_BAD_ARCHIVE = 13; 
    public const int ERAR_UNKNOWN_FORMAT = 14; 
    public const int ERAR_EOPEN = 15; 
    public const int ERAR_ECREATE = 16; 
    public const int ERAR_ECLOSE = 17; 
    public const int ERAR_EREAD = 18; 
    public const int ERAR_EWRITE = 19; 
    public const int ERAR_SMALL_BUF = 20; 

    public const int RAR_OM_LIST = 0; 
    public const int RAR_OM_EXTRACT = 1; 

    public const int RAR_SKIP = 0; 
    public const int RAR_TEST = 1; 
    public const int RAR_EXTRACT = 2; 

    public const int RAR_VOL_ASK = 0; 
    public const int RAR_VOL_NOTIFY = 1; 

    [DllImport("unrar.dll")] 
    public static extern IntPtr RAROpenArchive(ref RAROpenArchiveData ArchiveData); 

    [DllImport("unrar.dll")] 
    public static extern int RARCloseArchive(IntPtr hArcData); 

    [DllImport("unrar.dll")] 
    public static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData HeaderData); 

    [DllImport("unrar.dll")] 
    public static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx ArchiveData); 

    [DllImport("unrar.dll")] 
    public static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx HeaderData); 

    [DllImport("unrar.dll")] 
    public static extern int RARProcessFile(IntPtr hArcData, int Operation, string DestPath, string DestName); 

    [DllImport("unrar.dll")] 
    public static extern int RARGetDllVersion(); 

    [DllImport("user32.dll")] 
    private static extern bool CharToOem(string lpszSrc, [Out] StringBuilder lpszDst); 

    /// <summary> 
    ///  opens an arcive and unloads its content to destDolder 
    /// </summary> 
    /// <param name="strFileName">input .rar File</param> 
    /// <param name="destFolder">destination Folder</param> 
    public void UnloadArchieve(string strFileName, string destFolder) 
    { 
     IntPtr lHandle; 
     int iStatus; 
     RAROpenArchiveData uRAR = new RAROpenArchiveData(); 
     RARHeaderData uHeader = new RARHeaderData(); 

     uRAR.ArcName = strFileName + char.MinValue; 
     uRAR.OpenMode = RAR_OM_EXTRACT; 
     uRAR.CmtBuf = null; 

     string ExtractDir = Path.GetDirectoryName(strFileName); 
     StringBuilder sbDir = new StringBuilder(); 
     CharToOem(destFolder, sbDir); 

     lHandle = RAROpenArchive(ref uRAR); 
     if (uRAR.OpenResult != 0) 
      Console.Write("Unable to open the Archieve!!!"); 
     while (RARReadHeader(lHandle, ref uHeader) == 0) 
     { 
      int result = RARProcessFile(lHandle, 2, sbDir.ToString(), null); 
      if (0 != result) 
       Console.Write("Unable to open the Archieve!!!"); 
     } 


     iStatus = RARCloseArchive(lHandle); 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct RARHeaderData 
    { 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
     public string ArcName; 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
     public string FileName; 
     public uint Flags; 
     public uint PackSize; 
     public uint UnpSize; 
     public uint HostOS; 
     public uint FileCRC; 
     public uint FileTime; 
     public uint UnpVer; 
     public uint Method; 
     public uint FileAttr; 
     public string CmtBuf; 
     public uint CmtBufSize; 
     public uint CmtSize; 
     public uint CmtState; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct RAROpenArchiveData 
    { 
     public string ArcName; 
     public uint OpenMode; 
     public uint OpenResult; 
     public string CmtBuf; 
     public uint CmtBufSize; 
     public uint CmtSize; 
     public uint CmtState; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct RAROpenArchiveDataEx 
    { 
     public string ArcName; 
     public string ArcNameW; 
     public uint OpenMode; 
     public uint OpenResult; 
     public string CmtBuf; 
     public uint CmtBufSize; 
     public uint CmtSize; 
     public uint CmtState; 
     public uint Flags; 
     public uint Reserved; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct RARHeaderDataEx 
    { 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
     public string ArcName; 
     [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
     public string FileName; 
     public string FileNameW; 
     public uint Flags; 
     public uint PackSize; 
     public uint PackSizeHigh; 
     public uint UnpSize; 
     public uint UnpSizeHigh; 
     public uint HostOS; 
     public uint FileCRC; 
     public uint FileTime; 
     public uint UnpVer; 
     public uint Method; 
     public uint FileAttr; 
     public string CmtBuf; 
     public uint CmtBufSize; 
     public uint CmtSize; 
     public uint CmtState; 
     public uint Reserved; 
    } 
} 
+0

Können Sie uns den Code zur Verfügung stellen, der die Ausnahme auslöst? – whymatter

+0

Können Sie eine Beispieldatei '.rar' angeben? Ich dachte, Dropbox produziert nur Zip-Dateien. – smarx

+0

@whymatter ist nicht wie der Code funktioniert nicht, in fackt funktioniert es gut für jede andere .rar-Datei .. nur nicht die Automaten aus Dropbox ... ill immer noch die SharpCompress functon –

Antwort

0

Ok so scheint es, dass @smarx war richtig mit seiner Comme nicht! Es ist ein bisschen komisch, aber Dropbox erzeugt .rar-Dateien, die kein Rar, sondern einen Zip-Header enthalten. Dies ist kein Problem für irgendeine Form der automatischen Erkennung des Header-Formats, aber wenn Sie eine Rar-Extraktionsmethode verwenden (die für .rar-Dateien verwendet werden soll), wird es nicht funktionieren.