Android Question splitted zip

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello All,
I need to zip a 700MB file into several 100 Mb files, like file1.zip, file2.z01, file3.z02, etc..
So I can send it via HTTP Postmultipart

Is there any lib or any way to do it, in B4A ?

Thanks
 

Sandman

Expert
Licensed User
Longtime User
(I just want to mention that you probably don't need the zip function to split the file like that. I mean, in theory you could zip to a file that is 490 Mb, and then you just send that as individual 100 Mb parts. Which then are just merged back into a single file on the server.)
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
The file is 700Mb zipped. I get an 2Out of Memory error", since post multipart loads the file in memory prior to sending it.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Yes, I understand that. But I'm saying it should be possible to slice up the file into smaller pieces before sending them. A zip application wouldn't understand the individual slices, but once you merge them together on the server, it should decompress normally.
 
Upvote 0

zed

Well-Known Member
Licensed User
This code has not been tested.
I don't have a 700MB zip file and I don't have a server. Please try if it works.

Split File:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
   
    Private http As HttpJob
    Private FilePath As String
    Private FileName As String
    Private partSize As Int = 100 * 1024 * 1024 ' 100 Mo en octets
    Private partIndex As Int = 0
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    FilePath = "/mnt/sdcard/Download/"
    FileName = "MyFile.zip"
   
    SplitAndUpload(FilePath, FileName)
   
End Sub

Sub SplitAndUpload(F_Path As String, F_Name As String)
    Dim raf As RandomAccessFile
    raf.Initialize(F_Path, F_Name, False)
   
    Dim totalSize As Int = raf.Size
    Dim buffer(partSize) As Byte
    Dim offset As Int = 0
   
    Do While offset < totalSize
        Dim bytesRead As Int = Min(partSize, totalSize - offset)
        raf.ReadBytes(buffer, 0, bytesRead, offset)
           
        Dim partName As String = "part_" & partIndex & ".zip"
        Dim partPath As String = File.DirDefaultExternal & "/" & partName
        File.WriteBytes(partPath, partName, buffer)
           
        UploadFile(partPath, partName)
           
        offset = offset + bytesRead
        partIndex = partIndex + 1
    Loop
   
    raf.Close
End Sub

Sub UploadFile(partPath As String, partName As String)
    http.Initialize("upload", Me)
    http.PostMultipart("https://YouServer.com/upload", CreateMap("file" : partPath), Array(partName))
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Log("Upload successful : " & Job.GetString)
    Else
        Log("Error while uploading : " & Job.ErrorMessage)
    End If
    Job.Release
End Sub
 
Upvote 0
Top