ปัญหาในการถอดรหัสเอกสารจากอาร์เรย์ไบต์เพื่อเขียนลงไฟล์

ฉันกำลังดึงข้อมูลไบต์ [] จากระบบ SAP และพยายามถอดรหัสอาร์เรย์กลับเป็นรูปแบบไฟล์ต้นฉบับ (ทั้ง pdf, ข้อความ, jpg หรือ doc) อย่างไรก็ตาม หลังจากพยายามหลายครั้ง เมื่อเปิดไฟล์ชั่วคราวที่สร้างขึ้นใหม่ เนื้อหาของไฟล์จะเป็นค่าที่เข้ารหัส (ฉันใช้ไฟล์ข้อความสำหรับการทดสอบ)

รหัสที่ฉันใช้กำลังส่งคืนเส้นทางกลับไปยังวัตถุที่สร้างขึ้น และวิธีการของฉันอยู่ด้านล่าง

private string DecodeFromBytes(byte[] fileContents)
    {
        DocumentFilePath = System.IO.Path.GetTempFileName();
        //FileStream tempFile = File.OpenWrite(DocumentFilePath);

        string bytesString = Convert.ToBase64String(fileContents);
        
        // Used for Checking.
        // This is the exact same as the passed fileContents value. 
        byte[] convertedBytes = Convert.FromBase64String(bytesString);

        MemoryStream ms = new MemoryStream(convertedBytes, 0, convertedBytes.Length);

        ms.Write(convertedBytes, 0, convertedBytes.Length);

        using (FileStream tempFile = new FileStream(DocumentFilePath, FileMode.Create, FileAccess.Write))
        {
            //byte[] tempBytes = new byte[ms.Length];
            //tempFile.Write(convertedBytes, 0, convertedBytes.Length);
            ms.WriteTo(tempFile);
        }
        
        //tempFile.Write(fileContents, 0, fileContents.Length);
        //tempFile.Close();

        return DocumentFilePath;
    }

ข้อความที่เข้ารหัสที่กำลังบันทึกก็ไม่เหมาะสมเช่นกัน มีตัว A ตามหลัง ฉันเดาว่าเป็นช่องว่างภายในใช่ไหม? ด้านล่างนี้คือสิ่งที่กำลังถูกส่งคืน และสิ่งที่ ควร ที่ถูกส่งคืน

ข้อความที่เข้ารหัส

TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gSW50ZWdlciBuZWMgb2Rpby4gUHJhZXNlbnQgbGliZXJvLiANClNlZCBjdXJzdXMgYW5 0ZSBkYXBpYnVzIGRpYW0uIFNlZCBuaXNpLiBOdWxsYSBxdWlzIHNlbSBhdCBuaWJoIA0KZWxlbWVudHVtIGltcGVyZGlldC4gRHVpcyBzYWdpdHRpcyBpcHN1bS4gUHJhZXNlbnQgbWF1cmlzLiANCkZ1c2N lIG5lYyB0ZWxsdXMgc2VkIGF1Z3VlIHNlbXBlciBwb3J0YS4gTWF1cmlzIG1hc3NhLiBWZXN0aWJ1bHVtIGxhY2luaWEgYXJjdSBlZ2V0IG51bGxhLiANCkNsYXNzIGFwdGVudCB0YWNpdGkgc 29jaW9zcXUgYWQgbGl0b3JhIHRvcnF1ZW50IHBlciBjb251YmlhIG5vc3RyYSwgDQpwZXIgaW5jZXB0b3MgaGltZW5hZW9zLiBDdXJhYml0dXIgc29kYWxlcyBsaWd1bGEgaW4gbGliZXJvLgAAAAAAAAAA อร๊ายยยยย

สิ่งที่ควรอ่านไฟล์

Lorem ipsum dolor นั่ง amet, consectetur adipiscing elit. จำนวนเต็ม nec odio ปราเซนต์ ลิเบโร. Sed cursus ante dapibus diam. เสดนิส. Nulla quis sem ที่ nibh elementum imperdiet ดูส ซาจิตติส อิปซุม. แพรเซนท์ มอริส. Fusce nec tellus sed augue semper porta. มอริส มาสซา. ขนถ่าย lacinia arcu eget nulla Class aptent taciti soiosqu ad litora torkent per conubia nostra, per inceptos hisenaeos. Curabitur sodales ligula ใน libero

ฉันแน่ใจว่าฉันลืมขั้นตอนหนึ่ง ฉันแค่ไม่รู้ว่าอยู่ที่ไหน ความช่วยเหลือ / คำแนะนำใด ๆ ที่จะได้รับการชื่นชม

ขอบคุณ


person Jake_TheCoder    schedule 02.10.2014    source แหล่งที่มา
comment
เป็นการยากที่จะบอกว่าทำไมคุณจึงแปลงเป็น base64 เลย ทำไมคุณไม่ใช้แค่ File.WriteAllBytes(DocumentFilePath, fileContents)?   -  person Jon Skeet    schedule 02.10.2014
comment
ฉันกำลังแปลงเป็น Base64 เพื่อให้ผู้ใช้สามารถดูไฟล์ที่เลือกในแอปพลิเคชัน WPF เมื่อใช้ File.WriteAllBytes(DocumentFilePath, fileContents) ฉันยังคงส่งคืนข้อความที่เข้ารหัสด้านบนทุกประการ   -  person Jake_TheCoder    schedule 02.10.2014
comment
แต่คุณไม่ได้ใช้เวอร์ชัน base64 นอกเหนือจากการแปลงกลับเป็นไบต์...   -  person Jon Skeet    schedule 02.10.2014
comment
@JonSkeet คุณพูดถูก นั่นเป็นเหตุผลว่าทำไมฉันถึงรู้สึกเหมือนกำลังไล่ตามหางของฉัน! ฉันทำอะไรผิดในการพยายามเขียนไบต์ไปยังไฟล์ข้อความที่อ่านได้   -  person Jake_TheCoder    schedule 02.10.2014
comment
หากโค้ดปัจจุบันของคุณเขียนเป็นเวอร์ชันที่เข้ารหัส base64 ดูเหมือนว่าคุณกำลังถูกส่งในเวอร์ชันที่เข้ารหัส base64   -  person Jon Skeet    schedule 02.10.2014
comment
@JonSkeet คุณพูดถูกจริงๆ! ฉันกลับไปที่ซอร์สโค้ด SAP ของฉัน และพารามิเตอร์ที่ถูกส่งผ่านใช้ฟังก์ชัน code 'SSFC_BASE64_ENCODE' นั่นจะอธิบายการไล่ตามหาง   -  person Jake_TheCoder    schedule 02.10.2014


คำตอบ (1)


ตัวอักษร 'A' เหล่านี้มีค่าเท่ากับ 0 ดูเหมือนว่าอาร์เรย์ fileContents ของคุณจะมีความยาว 510 ไบต์ โดยมีเลขศูนย์ต่อท้าย นี่คือรหัสที่จำลองปัญหา:

var str = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. 
Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh 
elementum imperdiet. Duis sagittis ipsum. Praesent mauris. 
Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. 
Class aptent taciti sociosqu ad litora torquent per conubia nostra, 
per inceptos himenaeos. Curabitur sodales ligula in libero.";
// str's length is much lower
var bytes = new byte[510];
Encoding.UTF8.GetBytes(str, 0, str.Length, bytes, 0);
// Value of output is exactly the same as yours
var output = Convert.ToBase64String(bytes);

แต่รหัสนี้จะให้ผลลัพธ์ที่คาดหวังแก่คุณ:

var str = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. 
Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh 
elementum imperdiet. Duis sagittis ipsum. Praesent mauris. 
Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. 
Class aptent taciti sociosqu ad litora torquent per conubia nostra, 
per inceptos himenaeos. Curabitur sodales ligula in libero.";
var bytes = Encoding.UTF8.GetBytes(str);
var output = Convert.ToBase64String(bytes);
person Eldar Dordzhiev    schedule 02.10.2014
comment
รหัสของคุณให้สตริง Base64 ที่ถูกต้องแก่ฉัน และกำจัดเส้นทางที่น่ารำคาญเหล่านั้นออกไป ตอนนี้ ฉันกำลังพยายามหาวิธีบันทึกไบต์ลงในไฟล์ข้อความที่อ่านได้ (หรือ pdf ตามความจำเป็น) - person Jake_TheCoder; 02.10.2014
comment
@sithlordjake ฉันไม่แน่ใจเกี่ยวกับสิ่งที่คุณพยายามทำเลย วิธีการของคุณเรียกว่า DecodeFromBytes() แต่จริงๆ แล้วมันจะเข้ารหัส fileContents เป็น base64 - person Eldar Dordzhiev; 02.10.2014
comment
ฉันเชื่อว่าฉันมีวิสัยทัศน์ในอุโมงค์ สิ่งที่ฉันพยายามทำคือดึงข้อมูลไบต์ [] และเขียนไบต์ลงในไฟล์ชั่วคราวเพื่อแสดงวัตถุประสงค์ และสามารถดูได้อย่างเหมาะสม (เช่น ข้อความเป็นข้อความ รูปภาพต่อรูปภาพ PDF เป็น pdf) ฉันใช้วิธีนี้เพื่อสร้าง URL ชั่วคราวสำหรับการดูวัตถุของฉัน พารามิเตอร์อีกสองตัวของออบเจ็กต์คือนามสกุลไฟล์และชื่อเรื่อง - person Jake_TheCoder; 02.10.2014
comment
ดูเหมือนว่าจะได้ผลโดยใช้ตัวอย่างที่คุณให้ไว้ code FileStream fs = FileStream ใหม่ (DocumentFilePath, FileMode.Create, FileAccess.Write, FileShare.None); fs.Write (เอาต์พุต, 0, เอาต์พุตความยาว); fs.ปิด(); ฉันเดาว่าปัญหาอยู่ที่ไบต์ว่าง ดูเหมือนว่า SAP จะเพิ่ม 0 เพื่อขยายอาร์เรย์ ฉันจะลองวนซ้ำหรือคัดลอกอาเรย์ และตัดศูนย์ที่ละเมิดออก - person Jake_TheCoder; 02.10.2014