2017-07-19 24 views
-1

創建一個字節數組,一些幫助,將允許以下後:創建一個奇怪的字節數組我

  • 字節1-2:一個整數,n,指定文件名長度
  • 3 - N + 2:文件
  • n + 3中的名稱 - N + 10:文件
  • 的n + 11的最後修改日期 - N + 12:整數值爲1
  • N + 13 - n + 16:文件數據長度的長整數
  • n + 17 - n + 20:值爲0的長整數
  • n + 21 - end:文件的內容。

我已經有以下代碼將文件放入字節數組,但是這是在最後一部分。

byte[] filebytes; 
st.birth_certificate = detail[4]; 
downloadfile.HTML = detail[4]; 
downloadfile.fileName = downloadfile.GetFileNameFromUrl(st.birth_certificate); 
downloadfile.toLocation = @"c:\temp\" + downloadfile.fileName; 
if (downloadfile.DownloadFile()) 
{ 
    filebytes= File.ReadAllBytes(downloadfile.toLocation); 
    st.birth_certificate_file = filebytes; 
} 

任何幫助將不勝感激。

回答

0

與BinaryReader更好。我不確定數字是十六進制值還是ASCII數字(或Big/Little Endian),所以我正在做一些猜測。代碼可能需要一些小的調整:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string URL = "enter you url here"; 
      FileStream sReader = File.OpenRead(URL); 
      BinaryReader reader = new BinaryReader(sReader); 

      int filenameLength = reader.ReadInt16(); 
      string filename = Encoding.UTF8.GetString(reader.ReadBytes(filenameLength)); 
      int year = int.Parse(Encoding.UTF8.GetString(reader.ReadBytes(4))); 
      int month = int.Parse(Encoding.UTF8.GetString(reader.ReadBytes(2))); 
      int day = int.Parse(Encoding.UTF8.GetString(reader.ReadBytes(2))); 
      DateTime date = new DateTime(year, month, day); 
      short number1 = reader.ReadInt16(); 
      int number2 = reader.ReadInt32(); 
      byte[] data = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position + 1)); 

     } 


    } 
} 
+0

好的,我完全複製了你的代碼,如圖所示,因爲它似乎沒有工作。我已將「在此輸入URL」替換爲c:\\ temp \\ file_name.pdf,並在字符串文件名行發生錯誤。 我錯過了什麼 –

+0

檢查filenameLength的值。長度可能大於文件大小。我不知道實際的字節數是倒退(小/大端)還是長度是ascii。 – jdweng