Stenberg
Stenberg

Reputation: 25

Create a zip File From a Single file

Create a zip File From a Single file. I have start to build a new compression tool on vb.net and its works fine, but i have to make a directory first and insert my files into it and then compress. How can i make a compress file from a single file by press the button5.

I have try like this

ZipFileExtensions.CreateEntryFromFile(TextBox4.Text, TextBox3.Text, CompressionLevel.Optimal)

the textbox4 is the chose file and textbox3 is name for the file but it give me the error The value of type cannot be converted to System.IO.Compression.ZipArchive

This is my code

Imports System.IO
Imports System.IO.Compression

Public Class Form2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ProgressBar1.Show()
    Timer1.Start()
    TextBox2.Text = TextBox2.Text + "\" + TextBox3.Text
    ' Create ZIP from "source" directory (in program folder).
    ZipFile.CreateFromDirectory(TextBox1.Text,
                                TextBox2.Text + ".zip",
                                CompressionLevel.Optimal,
                                False)
    MessageBox.Show("The Process are complete", "MediaZip", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    ProgressBar1.Hide()
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim dialog As New FolderBrowserDialog()
    dialog.RootFolder = Environment.SpecialFolder.Desktop
    dialog.SelectedPath = "C:\"
    dialog.Description = "Select Application Configuration Files Path"
    If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
        TextBox1.Text = dialog.SelectedPath
    End If
    TextBox1.Text = dialog.SelectedPath

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim dialog As New FolderBrowserDialog()
    dialog.RootFolder = Environment.SpecialFolder.Desktop
    dialog.SelectedPath = "C:\"
    dialog.Description = "Select A Folder To Compress"
    If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
        TextBox2.Text = dialog.SelectedPath
    End If
    TextBox2.Text = dialog.SelectedPath
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ProgressBar1.Increment(1 * 55)
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.InitialDirectory = "C:\"
    OpenFileDialog1.FileName = "Select A File..."
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim sName As String = OpenFileDialog1.SafeFileName
        TextBox4.Text = OpenFileDialog1.FileName

    End If
End Sub

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

End Sub
End Class

Upvotes: 0

Views: 4742

Answers (1)

Stenberg
Stenberg

Reputation: 25

I have finally sort all the the errors and its working like i want. I make a temporary directory, move the chosen file into it, then compress and send it to the Desktop , and finally delete the temporary directory. This is the working code

Imports System.IO
Imports System.IO.Compression
Imports System
Imports System.Windows.Forms
Imports System.Net
Imports System.Diagnostics
Public Class Form4
Dim tempdir As String = "C:\MediaZip"

Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Form1.Hide()
    Me.BringToFront()
    IO.Directory.Delete(tempdir, True)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    ProgressBar1.Show()
    Timer1.Start()
    TextBox2.Text = TextBox2.Text + "\" + TextBox3.Text
    ' Create ZIP from "source" directory (in program folder).
    ZipFile.CreateFromDirectory("C:\MediaZip",
                                TextBox2.Text + ".zip",
                                CompressionLevel.Optimal,
                                False)
    MessageBox.Show("The Process are complete", "MediaZip", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    ProgressBar1.Hide()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim dialog As New FolderBrowserDialog()
    dialog.RootFolder = Environment.SpecialFolder.Desktop
    dialog.SelectedPath = "C:\"
    dialog.Description = "Were To Store Your File ?"
    If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
        TextBox2.Text = dialog.SelectedPath
    End If
    TextBox2.Text = dialog.SelectedPath
    Dim logDirectoryProperties As System.IO.DirectoryInfo

    My.Computer.FileSystem.CopyFile(
       TextBox1.Text,
       "C:\MediaZip\" + TextBox4.Text)
End Sub

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.InitialDirectory = "C:\"
    OpenFileDialog1.FileName = "File To Zip..."
    OpenFileDialog1.Multiselect = True
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim sName As String = OpenFileDialog1.SafeFileName
        TextBox1.Text = OpenFileDialog1.FileName
        TextBox4.Text = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
        IO.Directory.Delete(tempdir, True)
    End If
 End Sub
End Class

Upvotes: 1

Related Questions