如何创建自动备份文件和备份类型压缩。 但我想备份包含数据文件(.doc,.xls,.tiff,.pdf ...)但不包含sql数据库的文件夹。 我想要文件备份SHARE-AC.zip。但是现在有文件夹SHARE-AC不要压缩。
Imports System.IO
Imports System.IO.Compression
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dstr As String
Dim mstr As String
Dim ystr As String
Dim folstr As String
Dim dsumstr As String
dstr = DateTime.Today.ToString("dd")
mstr = DateTime.Today.ToString("MM")
ystr = DateTime.Today.ToString("yyyy")
dsumstr = ystr & "-" & mstr & "-" & dstr
folstr = "Y:\server1\Fileserver-" & dsumstr
Try
My.Computer.FileSystem.CreateDirectory(folstr)
My.Computer.FileSystem.CreateDirectory(folstr & "\SHARE-AC")
My.Computer.FileSystem.CopyDirectory("D:\SHARE-AC", folstr & "\SHARE-AC")
Label1.Text = "Back up DATE " & dsumstr & " Complete"
Catch ex As Exception
Label1.Text = (ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:0)
我没有得到您的问题,但如果您只想压缩文件,可以使用this
更新:您可以在System.IO.Compression命名空间中使用GZipStream
.NET 2.0。
Public Shared Sub CompressFile(path As String)
Dim sourceFile As FileStream = File.OpenRead(path)
Dim destinationFile As FileStream = File.Create(path & ".gz")
Dim buffer As Byte() = New Byte(sourceFile.Length - 1) {}
sourceFile.Read(buffer, 0, buffer.Length)
Using output As New GZipStream(destinationFile, CompressionMode.Compress)
Console.WriteLine("Compressing {0} to {1}.",sourceFile.Name,destinationFile.Name, False)
output.Write(buffer, 0, buffer.Length)
End Using
' Close the files.
sourceFile.Close()
destinationFile.Close()
End Sub
.NET 4
Public Shared Sub Compress(fi As FileInfo)
' Get the stream of the source file.
Using inFile As FileStream = fi.OpenRead()
' Prevent compressing hidden and
' already compressed files.
If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And fi.Extension <> ".gz" Then
' Create the compressed file.
Using outFile As FileStream = File.Create(Convert.ToString(fi.FullName) & ".gz")
Using Compress As New GZipStream(outFile, CompressionMode.Compress)
' Copy the source file into
' the compression stream.
inFile.CopyTo(Compress)
Console.WriteLine("Compressed {0} from {1} to {2} bytes.", fi.Name, fi.Length.ToString(), outFile.Length.ToString())
End Using
End Using
End If
End Using
End Sub
答案 1 :(得分:0)
如果要在您设置的目录上压缩文件,请使用以下代码:
Dim dstr As String
Dim mstr As String
Dim ystr As String
Dim folstr As String
Dim dsumstr As String
dstr = DateTime.Today.ToString("dd")
mstr = DateTime.Today.ToString("MM")
ystr = DateTime.Today.ToString("yyyy")
dsumstr = ystr & "-" & mstr & "-" & dstr
folstr = "Y:\server1\Fileserver-" & dsumstr
Try
My.Computer.FileSystem.CreateDirectory(folstr)
My.Computer.FileSystem.CreateDirectory(folstr & "\SHARE-AC")
ZipFile.CreateFromDirectory("D:\SHARE-AC", folstr & "\SHARE-AC\myfile.zip") ' Zip all file in your directory
'My.Computer.FileSystem.CopyDirectory("D:\SHARE-AC", folstr & "\SHARE-AC")
Label1.Text = "Back up DATE " & dsumstr & " Complete"
Catch ex As Exception
Label1.Text = (ex.Message)
End Try
End Sub
您需要进口:
Imports System.IO
Imports System.IO.Compression
<强>更新强>
您可以使用SharpZipLip 这里有一些Examples。