How to split the files of a folder into zip files of 2 mb

0

I tried to search for a solution but until now nothing could not find any solution to split the files of a folder into zip files of 2 mb. And when I try to send in a single file the software of the error me derived to the size of the zip.

How can I split the contents of a folder into 2 mb files? The operation timed out sending e-mail with zip file.

I'm developing an application to apply web cameras for forest and forest monitoring.
This application records photo frames and then we can make a video of these photo frames but I'll explain:

The application when we load the green button it starts to take photo frames, after having executed them and load in the red box to stop the software to take photo frames, then it does a zip file of these photo frames and need send them by e-mail and then delete all of their folder.

Well my problem is not in the software itself everything is working fine, but the problem starts when the software before deleting everything I want to send the zip file to the email but of course these files are wider than allowed.

Then give me an exception 'The operation timed out' I've tried it in several ways, but with no success.
I was wondering if there is any way to overcome this problem.

This is my code in question:

Imports System.IO
Imports System.Net.Mail

Imports System.IO.Compression
Imports System.Xml

Public Class Form2
Dim mydataandtimeforsave As String = DateTime.Now.ToString("yyyyMMddHHmmss")
Dim frame As Long 'individual frames
Dim tempdir As String = "C:\dirorg/" ' images temp directory
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If TextBox1.Text = "" Then
        TextBox1.Text = 1
    End If

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    capturex()
    'save()
End Sub
Public Sub capturex()
    Try

        Dim area As Rectangle
        Dim graph As Graphics
        Dim captured As Bitmap
        area = Screen.PrimaryScreen.Bounds
        captured = New System.Drawing.Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        graph = Graphics.FromImage(captured)

        graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
        ' you will need IF statement if you have checkbox
        If CheckBox1.Checked = True Then
            Cursor.Draw(graph, New Rectangle(New Point(Cursor.Position.X - Cursor.HotSpot.X, Cursor.Position.Y - Cursor.HotSpot.Y), Cursor.Size))

        End If
        Dim strings As String

        strings = frame
        captured.Save(tempdir & "\" & strings & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        frame += 1
        Label2.Text = "TIME: " & frame
    Catch ex As Exception

    End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    Me.Timer1.Interval = TimeSpan.FromSeconds(TextBox1.Text).TotalMilliseconds
    capturex()

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    renameFilesInFolder()


    '-framerate 3 -i "Imgp%%04d.jpg" -s 720x480 test.avi)
End Sub
Private Sub renameFilesInFolder()
    Dim sourcePath As String = "C:\dirorg/"
    Dim searchPattern As String = "*.jpg"
    Dim i As Integer = 0
    For Each fileName As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)
        File.Move(Path.Combine(sourcePath, fileName), Path.Combine(sourcePath, "Imgp" & i & "d.jpg"))
        i += 1
    Next
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    With SaveFileDialog1
        .DefaultExt = ".avi"
        .FilterIndex = 1
        .Filter = "Avi Files (*.avi)|*.avi|All files (*.*)|*.*"
    End With
    SaveFileDialog1.ShowDialog()
End Sub

Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

    Dim args As String 'declare args
    args = "-r 1/.1 -i " & "C:\dirorg" & "\%01d.jpg -c:v libx264 -r 80 -pix_fmt yuv420p " & Chr(34) & SaveFileDialog1.FileName & Chr(34) 'set ffmpeg arguments
    Dim proc As New Process
    Dim proci As New ProcessStartInfo
    proci.FileName = "C:\dirorg\ffmpeg.exe"
    proci.Arguments = args
    proci.WindowStyle = ProcessWindowStyle.Hidden
    proci.CreateNoWindow = True
    proci.UseShellExecute = False
    proc.StartInfo = proci
    proc.Start()
    Do Until proc.HasExited = True
        Me.Text = "Saving"
    Loop
    Me.Text = "your video done"

    MsgBox("Done")
    compimages()
    BackgroundWorker1.RunWorkerAsync()
    MsgBox("All Done")
    Dim directoryName As String = tempdir

    For Each deleteFile In Directory.GetFiles(directoryName, "*.jpg", SearchOption.TopDirectoryOnly)
        File.Delete(deleteFile)
    Next
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Timer1.Stop()
End Sub

Public Sub compimages()
    'To zip a directory
    Dim mydataandtimeforsave = DateTime.Now.ToString("yyyyMMddHHmmss")
    Dim Compressed = ZipDirectory("C:\dirorg")
    File.WriteAllBytes("C:\dirorg\images.zip", Compressed)
End Sub
Public Sub uncompimages()
    'To unzip a zipped file
    Dim Compressed = File.ReadAllBytes("C:\dirorg\images" & mydataandtimeforsave & ".zip")
    UnzipDirectory(Compressed, "C:\dirorg")
End Sub
Public Function ZipDirectory(DirPath As String) As Byte()
    If Not Directory.Exists(DirPath) Then Return Nothing

    Dim Directories = Directory.GetDirectories(DirPath, "*", SearchOption.AllDirectories)
    Dim Files = Directory.GetFiles(DirPath, "*", SearchOption.AllDirectories)

    Dim X As New XmlDocument
    Dim RootNode = X.CreateElement("Content")
    Dim DirsNode = X.CreateElement("Directories")
    Dim FilesNode = X.CreateElement("Directories")

    X.AppendChild(RootNode)
    RootNode.AppendChild(DirsNode)
    RootNode.AppendChild(FilesNode)

    For Each d In Directories
        Dim DirNode = X.CreateElement("Directory")
        Dim PathAttrib = X.CreateAttribute("Path")
        PathAttrib.Value = d.Replace(DirPath & "\", "") 'Create relative paths
        DirNode.Attributes.Append(PathAttrib)
        DirsNode.AppendChild(DirNode)
    Next

    For Each f In Files
        Dim FileNode = X.CreateElement("File")
        Dim PathAttrib = X.CreateAttribute("Path")
        PathAttrib.Value = f.Replace(DirPath & "\", "") 'Create relative paths
        FileNode.Attributes.Append(PathAttrib)
        FileNode.InnerText = Convert.ToBase64String(File.ReadAllBytes(f))
        FilesNode.AppendChild(FileNode)
    Next

    Using Mem As New MemoryStream()
        X.Save(Mem)
        Dim AllContentsAsByteArray = Mem.ToArray()
        Dim CompressedContent = CompressArray(AllContentsAsByteArray)
        Return CompressedContent
    End Using
End Function

Public Sub UnzipDirectory(compressed() As Byte, outputPath As String)
    If Not Directory.Exists(outputPath) Then Directory.CreateDirectory(outputPath)

    Dim Uncompressed = DecompressArray(compressed)

    Dim X As New XmlDocument

    Using Mem As New MemoryStream(Uncompressed)
        X.Load(Mem)

        Dim RootNode = X.FirstChild
        Dim DirsNode = RootNode.FirstChild
        Dim FilesNode = RootNode.FirstChild.NextSibling

        For Each ChildDir In DirsNode.ChildNodes
            Directory.CreateDirectory(Path.Combine(outputPath, DirectCast(ChildDir, XmlNode).Attributes.Item(0).Value))
        Next

        For Each ChildFile In FilesNode.ChildNodes
            Dim FilePath = Path.Combine(outputPath, DirectCast(ChildFile, XmlNode).Attributes.Item(0).Value)
            Dim Content = Convert.FromBase64String(DirectCast(ChildFile, XmlNode).InnerText)
            File.WriteAllBytes(FilePath, Content)
        Next
    End Using
End Sub

Private Function CompressArray(ByVal content() As Byte) As Byte()
    Using outFile As New MemoryStream()
        Using Compress As New GZipStream(outFile, CompressionMode.Compress)
            Compress.Write(content, 0, content.Length)
        End Using

        Return outFile.ToArray()
    End Using
End Function

Private Function DecompressArray(ByVal content() As Byte) As Byte()
    Using outFile As New MemoryStream()
        Using inFile As New MemoryStream(content)
            Using Compress As New GZipStream(inFile, CompressionMode.Decompress)
                Dim buffer(1023) As Byte
                Dim nRead As Integer
                Do
                    nRead = Compress.Read(buffer, 0, buffer.Length)
                    outFile.Write(buffer, 0, nRead)
                Loop While nRead > 0
            End Using
        End Using

        Return outFile.ToArray()
    End Using
End Function
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Using mail As New MailMessage
        mail.From = New MailAddress("[email protected]")
        mail.To.Add(My.Settings.destinationx$)
        mail.Body = My.Settings.bodyx$

        Dim attach As New Attachment("C:\dirorg\images.zip")
        mail.Attachments.Add(attach)

        mail.Subject = My.Settings.subjectx$
        mail.Priority = MailPriority.High
        Using SMTP As New SmtpClient

            SMTP.EnableSsl = True
            SMTP.Port = "587"
            SMTP.Host = "smtp.gmail.com"
            SMTP.Credentials = New Net.NetworkCredential("[email protected]", "u>#4TBOw")
            SMTP.Send(mail)
        End Using
    End Using

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MsgBox("Email has been sent successfully!", MsgBoxStyle.Information)
End Sub
End Class
    
asked by anonymous 17.08.2018 / 15:24

0 answers