从一个大的StorageFile中获取base64字符串并将其存储在另一个StorageFile中。

我有一个>4 GB的StorageFile,我想把它转换成一个base64字符串,并把这个字符串存储在另一个StorageFile中。

我有IBuffer来读取文件。

Dim tempFolder As StorageFolder = ApplicationData.Current.LocalFolder   
Dim ftemp As StorageFile = Await tempFolder.TryGetItemAsync(filename)
Dim ibuf As IBuffer = Await FileIO.ReadBufferAsync(ftemp)
Dim b64list As New List(Of String)
Dim b64string As String = String.Empty
Dim countins As Long = 0
Dim byt As Byte() = {}

If ibuf.Length > 500000000 Then

While ibuf.Length - countins > 500000000
byt = ibuf.ToArray(countins, 500000000) 'when countins exceeds 2 Gb, i get overflowexception: Arithmetic operation resulted in an overflow
Dim b1 As String = Convert.ToBase64String(byt, 0, 500000000)
b64list.Add(b1)
countins = countins + 500000000
End While

byt = ibuf.ToArray(countins, ibuf.Length - countins)
Dim b2 As String = Convert.ToBase64String(byt, countins, ibuf.Length - countins)
b64list.Add(b2)

else

b64string = Convert.ToBase64String(ibuf.ToArray())

End If

因为Byte()对象的大小限制在2Gb左右,我试图将缓冲区数组中存储的字节分割成其他更小的字节,并将其转换为一个base64字符串,然后将该字符串添加到一个字符串列表中,并通过该字符串列表将base64字符串写入文件。问题是当要读取和转换的字节偏移量超过2Gb大小时(请看代码中的注释!)。

有没有办法读取ibuffer中包含的2GB以上的字节?

非常感谢您

编辑Nico Zhu的解决方案非常完美! 非常感谢!这正是我所要找的。我试了一下6GB的文件,似乎效果很好,平均占用7GB的内存来完成操作。对于x86版本,我为打开的文件设置了2GB的限制,这不是一个问题。

我把我的vb.net代码版本粘贴到vb.net社区(测试到6Gb)。

Dim BufferList As List(Of IBuffer) = New List(Of IBuffer)()
Dim lclFolder As StorageFolder = ApplicationData.Current.LocalFolder   
Dim ftemp As StorageFile = Await lclFolder.TryGetItemAsync(filename)

Dim stredam = Await ftemp.OpenAsync(FileAccessMode.Read)
Dim size As ULong = stredam.Size
Dim splLength As UInteger = 1024 * 1024 * 128
Dim splCount = size / splLength
Dim res As IBuffer = Nothing

If size < splLength Then
res = Await FileIO.ReadBufferAsync(ftemp.generalfileselectedsmp)

Else

Dim splitCount = size / splLength

For i As Integer = 0 To CInt(splitCount) - 1
Dim postition = i * splLength

Using inputStream = stredam.GetInputStreamAt(CULng(postition))

Using dataReader = New Windows.Storage.Streams.DataReader(inputStream)
Dim numBytesLoaded As UInteger = Await dataReader.LoadAsync(splLength)
Dim buffder = dataReader.ReadBuffer(numBytesLoaded)
BufferList.Add(buffder)
End Using

End Using
Next

Dim leftLength = size Mod splLength

Using inputstream As IInputStream = stredam.GetInputStreamAt(splitCount * splLength)

Using dataReader As New DataReader(inputstream)
Dim numBytesLoaded As UInteger = Await dataReader.LoadAsync(CUInt(leftLength))
Dim leftBuffer = dataReader.ReadBuffer(numBytesLoaded)
BufferList.Add(leftBuffer)
End Using

End Using

Dim nnb As Long = 0

Dim tempFolder As StorageFolder = ApplicationData.Current.TemporaryFolder
Dim ftempdest As StorageFile = Await tempFolder.CreateFileAsync("tmpxencrxx0212", CreationCollisionOption.ReplaceExisting)

For Each ibb As IBuffer In BufferList
If ibb.Length > 0 Then

Await FileIO.AppendTextAsync(ftempdest, Convert.ToBase64String(ibb.ToArray()))

nnb = nnb + 1
End If
Next

解决方案:

有什么方法可以读取ibuffer中包含的2gb以上的字节?

对于你的要求,我们建议你把大文件切成片,用ibuffer列表来存储每个片。请注意您需要将应用目标平台设置为x64,有足够的内存来存储这种缓冲区。

private List<IBuffer> BufferList = new List<IBuffer>();

var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".zip");

Windows.Storage.StorageFile sampleFile = await picker.PickSingleFileAsync();
var stredam = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
ulong size = stredam.Size;
uint splLength = 1024 * 1024 * 128;
var splCount = size / splLength;

if (size < splLength)
{
  var res = FileIO.ReadBufferAsync(sampleFile);
}
else
{
    var splitCount = size / splLength;
    for (int i = 0; i < (int)splitCount; i++)
    {
        var postition = i * splLength;
        using (var inputStream = stredam.GetInputStreamAt((ulong)postition))
        {
            using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
            {
                uint numBytesLoaded = await dataReader.LoadAsync(splLength);
                var buffder = dataReader.ReadBuffer(numBytesLoaded);
                BufferList.Add(buffder);
            }
        }


    }
    var leftLength = size % splLength;
    using (var inputStream = stredam.GetInputStreamAt(splitCount * splLength))
    {
        using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
        {
            uint numBytesLoaded = await dataReader.LoadAsync((uint)leftLength);
            var leftBuffer = dataReader.ReadBuffer(numBytesLoaded);
            BufferList.Add(leftBuffer);
        }
    }

}

给TA打赏
共{{data.count}}人
人已打赏
未分类

翩翩医生发现的问题:安卓工具链--为安卓设备开发(安卓SDK版本29.0.3)。

2022-9-8 20:21:38

未分类

导航到搜索结果翩翩谷歌地图

2022-9-8 20:21:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索