這讓我在桌子上敲了一天的頭。我的任務是第一次構建 SSIS 包,它們需要在檔案放入目標檔案夾之前對檔案進行 PGP 加密。我將從腳本任務中遇到問題的代碼開始:
string keySource = "file:///C:/PubKeysSSIS/REDACTED/REDACTED.pubkey";
string outputDest = "file:///c:/REDACTED/REDACTED.txt";
string uriException = String.Empty;
Uri key;
Uri output;
if (!Uri.TryCreate(keySource, UriKind.RelativeOrAbsolute, out key))
uriException = String.Concat("Unable to load public key from ", key, "\n");
if (!Uri.TryCreate(outputDest, UriKind.RelativeOrAbsolute, out output))
uriException = String.Concat("Unable to load target for write command: ", outputDest, "\n");
if (!String.IsNullOrEmpty(uriException))
throw new InvalidParameterException(uriException);
using (MemoryStream outStream = new MemoryStream())
{
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
PgpUtilities.WriteFileToLiteralData(comData.Open(outStream), PgpLiteralData.Binary, new FileInfo(unencryptedData));
//other code that is unrelated below this point.
}
這些 URI 以絢麗的色彩傳遞 Uri.TryCreate() 呼叫,因此不會引發 InvalidParameterException。但是,當執行到達 PgPUtilities.WriteFileToLiteralData() 時,會拋出一個例外,說 URI 中有一個無效字符,這對我來說似乎很瘋狂,除非這是我一直在做某事的情況,我只是沒有看到它。
在較早的檢查中,我將這兩個字串與:
IEnumerable<char> invalid = Path.GetInvalidFileNameChars().Union(Path.GetInvalidPathChars());
然后,我能夠確定該函式令人毛骨悚然的是冒號。當我嘗試只使用驅動器號而不是 file:/// 標注的路徑時,這是真的,所以我認為它是驅動器號冒號。但是,當我從驅動器號中洗掉冒號時,我似乎仍然使用 file:/// 來獲取它。在搜索資源后,我在 SO 或 CodeProject 或類似網站上沒有找到任何涉及此問題的內容。我做錯了什么?
uj5u.com熱心網友回復:
我的評論是正確的;問題是傳遞了什么以及使用的方法。解決方案是將壓縮分解為它自己的功能并使用流進行管理。
private static byte[] Compress(byte[] data, string filename, CompressionAlgorithmTag algorithm)
{
using (MemoryStream compressionStream = new MemoryStream())
{
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm);
using (Stream cos = comData.Open(compressionStream))
{
PgpLiteralDataGenerator pldg = new PgpLiteralDataGenerator();
using (Stream cOut = pldg.Open(cos, PgpLiteralData.Binary, filename, data.Length, DateTime.UtcNow))
{
cOut.Write(data, 0, data.Length);
}
}
return compressionStream.ToArray();
}
}
我現在收到一個涉及不受支持的方法的錯誤,但我相信這是因為在 SSIS 中執行并且與此錯誤無關,所以我將這個答案發布給遇到同樣問題的其他人。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516539.html
標籤:C#验证西斯乌里充气城堡
