Jump to content
Bruno Neves

Sistema de Backup

Recommended Posts

Bruno Neves

Boas pessoal,

é o seguinte, eu tou a fazer um sistema de backup aqui no meu estagio.

E quero fazer o backup do que o utilizador quizer, o codigo que eu tenho so me permite fazer backup de uma pasta apenas só com ficheiros lá dentro.

Alguem me pode dizer onde eu estou a errar? é que eu preciso de fazer o backup das pastas que o utilizador quiser mais as sub pastas e por ai fora...

''' <summary>
''' Pick a file path to zip files
''' </summary>
Private Sub btnZipSelectFilePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZipSelectFilePath.Click
 Dim tOpenDirectory As New FolderBrowserDialog
 Try
	 If tOpenDirectory.ShowDialog = Windows.Forms.DialogResult.OK Then
		 txtSourceSafePath.Text = tOpenDirectory.SelectedPath
	 End If
 Catch ex As Exception
 End Try
 Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
 strFolderToSearch = txtSourceSafePath.Text
 objFSO = CreateObject("Scripting.FileSystemObject")
 objRootFolder = objFSO.GetFolder(strFolderToSearch)
 colSubfolders = objRootFolder.SubFolders
 For Each objFolder In colSubfolders
	 strOutput = strOutput & objFolder.name
	 strOutput = strOutput & vbCrLf
 Next
 MsgBox(strOutput)
End Sub

Este é o codigo dque obtem a pasta.

Private Sub btnZipFiles_Click(sender As System.Object, e As System.EventArgs) Handles btnZipFiles.Click
 If ZipFiles(txtSourceSafePath.Text, txtZipFileName.Text, txtSearchPattern.Text) Then
	 MessageBox.Show("All files zipped")
 End If
End Sub

Este é o codigo do botao para iniciar o backup.

''' <summary>
''' Zip all files currently in folder using search pattern
''' </summary>
Private Function ZipFiles(ByVal pFolderPath As String, ByVal pFileName As String, ByVal pSearchPattern As String) As Boolean
 Dim tOpenDirectory As DirectoryInfo = Nothing
 Try
	 tOpenDirectory = New DirectoryInfo(pFolderPath)
	 If tOpenDirectory.Parent.GetFiles.Length > 0 Then
		 'Zip file
		 ZipCompress.zipFiles(pFolderPath, String.Format("{0}\{1}.zip", txtdestino.Text, pFileName), pSearchPattern)
	 End If
	 Return True
 Catch ex As Exception
	 Return False
	 MessageBox.Show(ex.Message)
 End Try
End Function

Este é o codigo no form para fazer o Zip.

Public Class ZipCompress
''' <summary>
''' Zip files in directory to sZipFileName
''' </summary>
Friend Shared Sub zipFiles(ByVal sSourceDir As String, ByVal sZipFileName As String, ByVal SearchPattern As String)
 Dim astrFileNames() As String = Directory.GetFiles(sSourceDir.ToString, SearchPattern)
 Dim objCrc32 As New Crc32
 Dim strmZipOutputStream As ZipOutputStream
 Dim objZipEntry As ZipEntry
 Try
	 strmZipOutputStream = New ZipOutputStream(File.Create(sZipFileName.ToString))
	 strmZipOutputStream.SetLevel(6)
	 REM Compression Level: 0-9
	 REM 0: no(Compression)
	 REM 9: maximum compression
	 Dim strFile As String
	 For Each strFile In astrFileNames
		 Dim strmFile As FileStream = File.OpenRead(strFile)
		 Dim abyBuffer(CType(strmFile.Length - 1, Integer)) As Byte
		 strmFile.Read(abyBuffer, 0, abyBuffer.Length)
		 objZipEntry = New ZipEntry(Path.GetFileName(strFile))
		 objZipEntry.DateTime = DateTime.Now
		 objZipEntry.Size = strmFile.Length
		 strmFile.Close()
		 objCrc32.Reset()
		 objCrc32.Update(abyBuffer)
		 objZipEntry.Crc = objCrc32.Value
		 strmZipOutputStream.PutNextEntry(objZipEntry)
		 strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)
	 Next
		 strmZipOutputStream.Finish()
		 strmZipOutputStream.Close()
 Catch ex As Exception
	 Throw
 End Try
End Sub
End Class

Este é o codigo da Class para fazer o zip

Edited by Rui Carlos
Formatação do código.

Share this post


Link to post
Share on other sites
Nelson Sousa

Usando uma FolderBrowserDialog para obter a pasta, só vai poder capturar uma pasta de cada vez.

O melhor seria carregar um controle TreeView com sua árvore de pastas, e, ir selecionado as que lhe interessarem.

Não sei se é permitido passar links aqui no fórum, por isso vou evitar. Mas uma pesquisa no google vai lhe revelar vários que ensinam a usar o TreeView.


Um Abraço,

Nelson Sousa

Share this post


Link to post
Share on other sites
Bruno Neves

eu nao queria fazer bem como ta ai. eu queria fazer com uma grelha ou assim.

algu tipo:

9om.png

o utilizador clica na checkbox documentos e aparece o caminho dos docoumentos na listbox e depois o zip é feito dos caminhos que tem na listbox.

contudo nao me faz o zip das sub pastas :s

se me poder dar uma ajudinha com um exemplo ou assim agradecia.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.