You must Sign In to post a response.
  • Category: .NET

    How to compress folder in vb.net

    hello sir i want to know that how to compress folder in vb.net.Actually i want to compress image folder.
    Sir how to do this.
  • #724939
    hey avikash..
    you can use this code for that..

    using rar utility compressing folder like this

    Public Sub CreateRar()

    Dim strFile As String

    Dim strSDir As String

    Dim cmd, strDestDir As String

    strDestDir = System.Configuration.ConfigurationManager.AppSettings("TempLoaPath") + "\"

    strFile = "AllFiles"

    strSDir = strDestDir + "\AllFiles"
    strSDir = strSDir.Replace("\\", "\")

    If Not Proc Is Nothing Then

    Proc.Kill()

    End If
    cmd = "a" + " " + strDestDir + strFile + " " + strSDir

    Proc = New Process
    With Proc.StartInfo.FileName = System.Configuration.ConfigurationManager.AppSettings("WinRarPath")

    .Arguments = cmd

    .UseShellExecute = False

    .RedirectStandardOutput = True

    .CreateNoWindow = True

    End With

    Proc.EnableRaisingEvents = True

    Proc.Start()

    Proc.WaitForExit()

    Proc.BeginOutputReadLine()

    'dalc.CreateRar(strDestDir, strFile, strSDir)

    End Sub

    and also you can use this links for that..
    http://www.c-sharpcorner.com/uploadfile/scottlysle/compress-folders-with-visual-basic-and-the-sharpziplib-in-vb/

    http://www.nullskull.com/q/10036024/zipping-folder-in-vbnet.aspx

    http://www.vb-helper.com/howto_2005_compress_folder.html


    Thanks
    ketan

  • #724946
    This samle code might help you to zip the file.


    private void Zipper()
    {
    Try
    'Launch the zip.exe console app to do the actual zipping
    Dim i As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + "zip.exe")
    i.CreateNoWindow = True
    Dim args As String = ""
    Dim dest As String
    Dim strSource As String

    strSource = Application.StartupPath + "\\New"
    if (strSource.IndexOf(" ") <> -1)
    {
    'we got a space in the path so wrap it in double qoutes
    args += "\"" + strSource + "\""
    }
    else
    {
    args += strSource
    }

    strFileName = strXmlFileName.Substring(strXmlFileName.LastIndexOf("\\") + 1)
    strRootFolderName = strXmlFileName.Replace(strFileName, "")
    dest = strRootFolderName
    if (dest.EndsWith("\") = False)
    {
    dest += "\"
    }

    '//Make sure the zip file name ends with a zip extension
    'if(dest.EndsWith(".ZIP") == false)
    '{
    ' dest += ".zip";
    '}

    dest += strZipNo + ".zip"

    if(dest.IndexOf(" ") <> -1)
    {
    'we got a space in the path so wrap it in double qoutes
    args += " " + "\"" + dest + "\""
    }
    else
    {
    args += " " + dest
    }

    i.Arguments = args


    'Checking For Already Exist
    Dim finfoZip As FileInfo = New FileInfo(dest)
    if (finfoZip.Exists)
    {
    finfoZip.Delete()
    }

    '//Mark the process window as hidden so that the progress copy window doesn't show
    'i.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    'System.Diagnostics.Process p = System.Diagnostics.Process.Start(i);
    'p.WaitForExit();
    'blnZip = true;

    '*************** New Zip Function
    ZipNew(strSource, dest)
    blnZip = True
    gintZipCount += 1

    'Deleting the Temp Directory
    DeleteXMLFile(1)
    Catch ex As Exception
    MessageBox.Show("Error while Ziping" + strFileName + "file." + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    toolStripProgressBar1.Maximum = 0
    blnZip = False
    End Try
    }



    ***********************************************************************************************************************

    private void ZipNew(String strSrc,string strDes)
    {

    Dim emptyzip() As Byte = New Byte() {80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}


    Dim fs As FileStream = File.Create(strDes)
    fs.Write(emptyzip, 0, emptyzip.Length)
    fs.Flush()
    fs.Close()
    fs = Nothing

    'Copy a folder and its contents into the newly created zip file
    Dim sc As Shell32.ShellClass = New Shell32.ShellClass()
    Dim SrcFlder As Shell32.Folder = sc.NameSpace(strSrc)
    Dim DestFlder As Shell32.Folder = sc.NameSpace(strDes)
    Dim items As Shell32.FolderItems = SrcFlder.Items()



    'Count the number of FolderItems in the original source location
    Dim OriginalItemCount As Integer = RecurseCount(items)

    'Start the ziping
    DestFlder.CopyHere(items, 1024)

    'Timeout period... if the compression is not done within this time
    'limit then the zip.exe shuts down and the ziping is stoped
    Dim timeoutDeadline As DateTime = DateTime.Now.AddMinutes(30)

    'Wait until the ziping is done.
    for()
    {
    'Are we past the deadline?
    if(DateTime.Now > timeoutDeadline)
    {
    break
    }

    'Check the number of items in the new zip to see if it matches
    'the number of items in the original source location

    'Only check the item count every 5 seconds
    System.Threading.Thread.Sleep(5000)

    if(OriginalItemCount = RecurseCount(DestFlder.Items()))
    {
    break
    }
    }
    }


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #725021
    I recommanded below code for How to compress folder in vb.net
    Public Sub CreateRarfile()

    Dim stringFile,Cammand,stringSDir, stringDestDir As String

    strDestDir = System.Configuration.ConfigurationManager.AppSettings("TempLoaPath") + "\"

    stringfile = "AllFiles"

    stringSDir = strDestDir + "\AllFiles"
    stringSDir = stringSDir.Replace("\\", "\")

    Process.Kill()
    End If
    Cammand = "a" + " " + stringDestDir + stringfile + " " + stringSDir
    Process = New Processess
    With Process.StartInfo.FileName = System.Configuration.ConfigurationManager.AppSettings("WinRarPath")

    .Arguments = Cammand

    .UseShellExecute = False

    .RedirectStandardOutput = True

    .CreateNoWindow = True

    End With

    Process.EnableRaisingEvents = True

    Processess.Start()

    Process.WaitForExit()

    Process.BeginOutputReadLine()

    End Sub

    For More sample example

    http://www.nullskull.com/a/865/zip--unzip-folders-and-files-with-c.aspx

    http://forums.asp.net/t/1335524.aspx

  • #725132
    Refer below sample code to zip a file


    Imports System.IO
    Imports Ionic.Zip

    Namespace ArchieveFiles
    Public Partial Class Form1
    Inherits Form
    Public Sub New()
    InitializeComponent()
    End Sub
    Private Sub btnOpen1_Click(sender As Object, e As EventArgs)
    Dim fDialog As New OpenFileDialog()
    fDialog.Title = "Select file to be zip"
    'fDialog.Filter = "JPEG Files|*.jpeg|GIF Files|*.gif";
    If fDialog.ShowDialog() = DialogResult.OK Then
    textBox1.Text = fDialog.FileName.ToString()
    End If
    End Sub

    Private Sub btnZip1_Click(sender As Object, e As EventArgs)
    If textBox1.Text = "" Then
    MessageBox.Show("select file to be Zip", "Alert")
    Return
    End If

    Dim saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.Filter = "zip files (*.zip)|*.zip|rar files (*.rar)|*.rar"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
    'Zip one file
    Dim filename As String = textBox1.Text

    Try
    '** ZIP Single file - START HERE


    Using zip = New ZipFile()
    'Set password for your zip file
    zip.Password = "test123"
    zip.AddFile(filename, "")
    zip.Save(saveFileDialog1.FileName.ToString())
    End Using

    ' extract entries that use encryption
    Using zip As ZipFile = ZipFile.Read(saveFileDialog1.FileName.ToString())
    'check password before file is extract
    'zip.Password = "test123";

    zip.ExtractAll("extractDir")
    End Using
    '** ZIP Single file - END HERE

    MessageBox.Show("Files are Zipped Successfully!", "Alert")
    Catch ex As Exception
    MessageBox.Show("Error during Zip operation!", "Alert")
    End Try
    clear()
    End If
    End Sub
    Private Sub clear()
    textBox1.Text = ""
    textBox2.Text = ""
    textBox3.Text = ""
    End Sub

    Private Sub label6_Click(sender As Object, e As EventArgs)

    End Sub
    End Class
    End Namespace

    Regards
    N.Ravindran
    Your Hard work never fails


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.