Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Arkiviation - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.IO
  2. Public Class frmArk
  3.     Public SaveFile As StreamWriter
  4.     Public NodeCount As Int32 = 0
  5.     Public AllowedFiles As String = "*.txt;*.html;*.htm;*.ini;*.inf;*.css;*.xml;*.vb;*.designer;*.vbproj;*.cs;*.#d;*.c;*.cpp;*.h;*.pas;*.js;*.asm;*.asp;*.bat;*.bak;*.bin;*.dat;*.dev;*.frm;*.bas;*.vbp;*.vbx;*.log;*.php;*.aspx;*.vba;*.sln;*.suo"
  6. #Region "Methods"
  7.     Public Function GetDirName(ByVal Dir As String) As String
  8.         Return Dir.Remove(0, Dir.LastIndexOf("\") + 1)
  9.     End Function
  10.     'Procedure that fills the treeview
  11.     Public Sub AddDirectory(ByVal Dir As String, ByRef SelectedNode As TreeNode)
  12.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  13.         Dim Files() As String = Directory.GetFiles(Dir)
  14.         Dim S As String
  15.  
  16.         If SelectedNode Is Nothing Then
  17.             Dim N As TreeNode
  18.             SelectedNode = lstFiles.Nodes.Add(GetDirName(Dir))
  19.             SelectedNode.Tag = Dir
  20.  
  21.             For Each S In Files
  22.                 If AllowedFiles.Contains("*" + Path.GetExtension(S)) Then
  23.                     N = SelectedNode.Nodes.Add(Path.GetFileName(S))
  24.                     N.ImageIndex = 1
  25.                     N.Tag = S
  26.                     NodeCount += 1
  27.                 End If
  28.             Next
  29.  
  30.             If Dirs.Length > 0 Then
  31.                 For Each S In Dirs
  32.                     AddDirectory(S, SelectedNode)
  33.                 Next
  34.             End If
  35.         Else
  36.             Dim N, F As TreeNode
  37.             N = SelectedNode.Nodes.Add(GetDirName(Dir))
  38.             N.Tag = Dir
  39.  
  40.             For Each S In Files
  41.                 If AllowedFiles.Contains("*" + Path.GetExtension(S)) Then
  42.                     F = SelectedNode.Nodes(SelectedNode.Nodes.Count - 1).Nodes.Add(Path.GetFileName(S))
  43.                     F.ImageIndex = 1
  44.                     F.Tag = S
  45.                     NodeCount += 1
  46.                 End If
  47.             Next
  48.  
  49.             If Dirs.Length > 0 Then
  50.                 For Each S In Dirs
  51.                     AddDirectory(S, N)
  52.                 Next
  53.             End If
  54.         End If
  55.     End Sub
  56.     'Writes a new file
  57.     Public Sub WriteFile(ByVal Name As String)
  58.         Try
  59.             Dim R As New StreamReader(Name)
  60.             Dim FSize As Double = FileLen(Name)
  61.             Dim S As String
  62.             Dim Line As Double = 0
  63.  
  64.             lblStatus.Text = "Copia di " + Path.GetFileName(Name) + " in corso..."
  65.  
  66.             SaveFile.WriteLine("<file::" + Path.GetFileName(Name) + ">")
  67.  
  68.             While Not R.EndOfStream
  69.                 S = R.ReadLine
  70.                 SaveFile.WriteLine(S)
  71.                 Line += S.Length
  72.                 If Line > FSize Then
  73.                     Line = FSize
  74.                 End If
  75.                 Line += 1
  76.                 prgFile.Value = Line * 100 / FSize
  77.                 lblFile.Text = "Copying files: " & CInt(prgFile.Value) & "%"
  78.                 Application.DoEvents()
  79.             End While
  80.  
  81.  
  82.  
  83.             R.Close()
  84.             R = Nothing
  85.             prgFile.Value = 0
  86.             prgAll.Value += 1
  87.         Catch EX As Exception
  88.             MsgBox(Path.GetFileName(Name) + ": access denied!", MsgBoxStyle.Exclamation)
  89.         End Try
  90.     End Sub
  91.     'Saves the treeview
  92.     Public Sub Save(ByVal Node As TreeNode)
  93.         Dim File As TreeNode
  94.         Static Count As Int16 = 0
  95.  
  96.         lblStatus.Text = "Arkiviation's now working..."
  97.  
  98.         If Node.Nodes.Count = 0 Then
  99.             If Path.HasExtension(Node.Tag) Then
  100.                 WriteFile(Node.Text)
  101.             Else
  102.                 SaveFile.WriteLine("<dir::" + Node.Text + ">")
  103.                 SaveFile.WriteLine("</dir>")
  104.             End If
  105.             Exit Sub
  106.         End If
  107.  
  108.         SaveFile.WriteLine("<dir::" + Node.Text + ">")
  109.         For Each File In Node.Nodes
  110.             If File.Nodes.Count = 0 And Path.HasExtension(File.Tag) Then
  111.                 WriteFile(File.Tag)
  112.                 Count += 1
  113.                 lblAll.Text = "Total compression: " & CInt(Count * 100 / NodeCount) & "%"
  114.             Else
  115.                 Save(File)
  116.             End If
  117.         Next
  118.         SaveFile.WriteLine("</dir>")
  119.     End Sub
  120.     'Reads an archive file and insert its content in the treeview
  121.     'Creates also a copy of all files in the Temp folder
  122.     Public Sub OpenArk(ByVal FileName As String)
  123.         Dim R As New IO.StreamReader(FileName)
  124.         Dim W As StreamWriter
  125.         Dim S, Dir As String
  126.         Dim N As New TreeNode(Path.GetFileName(FileName))
  127.         Dim Size, Index As Double
  128.  
  129.         prgAll.Maximum = 100
  130.         Size = FileLen(FileName)
  131.         Dir = Application.StartupPath + "\Temp"
  132.  
  133.         While Not R.EndOfStream
  134.             S = R.ReadLine
  135.             If S.StartsWith("<file::") Or S.StartsWith("<dir::") Or S.StartsWith("</dir>") Then
  136.                 If S.StartsWith("<file::") Then
  137.                     If Not W Is Nothing Then
  138.                         W.Close()
  139.                     End If
  140.                     S = S.Remove(0, "<file::".Length)
  141.                     S = S.Remove(S.Length - 1, 1)
  142.                     W = New StreamWriter(Dir + "\" + S)
  143.                     AddNodeWithInfo(N, Dir + "\" + S)
  144.                 End If
  145.                 If S.StartsWith("<dir::") Then
  146.                     If Not W Is Nothing Then
  147.                         W.Close()
  148.                     End If
  149.                     S = S.Remove(0, "<dir::".Length)
  150.                     S = S.Remove(S.Length - 1, 1)
  151.                     Dir += "\" + S
  152.                     Directory.CreateDirectory(Dir)
  153.                     N = N.Nodes.Add(GetDirName(S))
  154.                 End If
  155.                 If S.StartsWith("</dir>") Then
  156.                     If Not W Is Nothing Then
  157.                         W.Close()
  158.                     End If
  159.                     Dir = Dir.Remove(Dir.LastIndexOf("\"), Dir.Length - Dir.LastIndexOf("\"))
  160.                     N = N.Parent
  161.                 End If
  162.             Else
  163.                 W.WriteLine(S)
  164.             End If
  165.             Index += S.Length
  166.             prgAll.Value = Index * 100 / Size
  167.             lblAll.Text = "Stock: " & CInt(Index * 100 / Size) & "%"
  168.             Application.DoEvents()
  169.         End While
  170.  
  171.         prgAll.Value = 0
  172.         lblAll.Text = "Completed"
  173.         lblFile.Text = "Completed"
  174.         lstFiles.Nodes.Add(N)
  175.     End Sub
  176.     Public Sub AddNodeWithInfo(ByVal N As TreeNode, ByVal FileName As String)
  177.         Dim G As TreeNode = N.Nodes.Add(Path.GetFileName(FileName))
  178.         G.ImageIndex = 1
  179.     End Sub
  180.     Public Function GetDirSize(ByVal Dir As String) As Double
  181.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  182.         Dim Files() As String = Directory.GetFiles(Dir)
  183.         Dim S As String
  184.         Dim Size As Double
  185.  
  186.         lblStatus.Text = "Evaluating directory size..."
  187.  
  188.         For Each S In Files
  189.             Size += FileLen(S)
  190.         Next
  191.         For Each S In Dirs
  192.             Size += GetDirSize(S)
  193.         Next
  194.  
  195.         Return Size
  196.     End Function
  197.     Public Sub CopyDirectory(ByVal Dir As String, ByVal Destination As String)
  198.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  199.         Dim Files() As String = Directory.GetFiles(Dir)
  200.         Dim S As String
  201.         Dim Size As Double = GetDirSize(Dir)
  202.         Dim Index As Double
  203.  
  204.         lblStatus.Text = "Copying files..."
  205.  
  206.         Try
  207.             MkDir(Destination)
  208.         Catch IOE As IOException
  209.             'MsgBox("Errore di accesso alla cartella: assicurarsi che non esista una cartella con lo stesso nome e di non " + _
  210.             '"stare modificando la suddetta mentre vengono eseguite le azioni di copia, quindi riprovare.", MsgBoxStyle.Exclamation)
  211.             MsgBox("An error has occurred while opening folder: make you sure that don't exist a folder with the same name and " + _
  212.             "that you aren't currently working on this folder while the operations are being executed. Try again after have done these controls.", MsgBoxStyle.Exclamation)
  213.         End Try
  214.         For Each S In Files
  215.             Application.DoEvents()
  216.             FileCopy(S, Destination + "\" + Path.GetFileName(S))
  217.             Index += FileLen(S)
  218.             lblAll.Text = "Copia dei file: " & CInt(Index * 100 / Size) & "%"
  219.             prgAll.Value = CInt(Index * 100 / Size)
  220.         Next
  221.         For Each S In Dirs
  222.             CopyDirectory(S, Destination + "\" + GetDirName(S))
  223.         Next
  224.     End Sub
  225.     Public Sub DeleteDirectory(ByVal Dir As String)
  226.         Dim Dirs() As String = Directory.GetDirectories(Dir)
  227.         Dim Files() As String = Directory.GetFiles(Dir)
  228.         Dim S As String
  229.         Dim Size As Double = GetDirSize(Dir)
  230.         Static Index As Double = 0
  231.  
  232.         Try
  233.             For Each S In Files
  234.                 Index += FileLen(S)
  235.                 File.Delete(S)
  236.                 lblAll.Text = "Deleting files: " & CInt(Index * 100 / Size)
  237.                 prgAll.Value = CInt(Index * 100 / Size)
  238.             Next
  239.         Catch IOE As Exception
  240.             MsgBox("An error has occurred while Arkiviation was deleting temporary files!", MsgBoxStyle.Exclamation)
  241.         End Try
  242.  
  243.         For Each S In Dirs
  244.             DeleteDirectory(S)
  245.         Next
  246.         Directory.Delete(Dir)
  247.     End Sub
  248. #End Region
  249.     Private Sub strAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAddFiles.Click, strAdd.ButtonClick
  250.         Dim FOpen As New Windows.Forms.OpenFileDialog
  251.         FOpen.Filter = "Text file|" + AllowedFiles
  252.         FOpen.Multiselect = True
  253.         If FOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
  254.             Dim S() As String = FOpen.FileNames
  255.             Dim File As String
  256.  
  257.             If lstFiles.SelectedNode Is Nothing Then
  258.                 Dim N As TreeNode = lstFiles.Nodes.Add(GetDirName(Path.GetDirectoryName(S(0))))
  259.                 Dim F As TreeNode
  260.                 For Each File In S
  261.                     F = N.Nodes.Add(Path.GetFileName(File))
  262.                     F.ImageIndex = 1
  263.                     F.Tag = File
  264.                 Next
  265.             Else
  266.                 Dim N As TreeNode
  267.                 For Each File In S
  268.                     N = lstFiles.SelectedNode.Nodes.Add(Path.GetFileName(File))
  269.                     N.ImageIndex = 1
  270.                     N.Tag = File
  271.                 Next
  272.             End If
  273.  
  274.             NodeCount += S.Length
  275.         End If
  276.         strArk.Enabled = True
  277.     End Sub
  278.     Private Sub strRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRemoveSelected.Click, strRemove.ButtonClick
  279.         If lstFiles.SelectedNode Is Nothing Then
  280.             MsgBox("No element selected!", MsgBoxStyle.Exclamation)
  281.             Exit Sub
  282.         End If
  283.         If MsgBox("Are you sure you want to remove the selected element?", MsgBoxStyle.Question + vbYesNo) = MsgBoxResult.No Then
  284.             Exit Sub
  285.         End If
  286.  
  287.         lstFiles.Nodes.Remove(lstFiles.SelectedNode)
  288.  
  289.     End Sub
  290.     Private Sub strClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strClear.Click
  291.         If MsgBox("Are you sure you want to remove all list elements?", 36) = MsgBoxResult.No Then
  292.             Exit Sub
  293.         End If
  294.  
  295.         lstFiles.Nodes.Clear()
  296.         NodeCount = 0
  297.         strExtract.Enabled = False
  298.         strArk.Enabled = False
  299.         strTools.Enabled = False
  300.         DeleteDirectory(Application.StartupPath + "\Temp")
  301.         strTools.Enabled = True
  302.     End Sub
  303.     Private Sub strArk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strArk.Click
  304.         Dim FSave As New Windows.Forms.SaveFileDialog
  305.         FSave.Filter = "Archive files|*.ark"
  306.         If FSave.ShowDialog = Windows.Forms.DialogResult.OK Then
  307.             Dim N As TreeNode
  308.             strArk.Enabled = False
  309.             prgAll.Maximum = NodeCount
  310.             SaveFile = New StreamWriter(FSave.FileName)
  311.             For Each N In lstFiles.Nodes
  312.                 Save(N)
  313.             Next
  314.             SaveFile.Close()
  315.             strArk.Enabled = True
  316.  
  317.             prgFile.Value = 0
  318.             prgAll.Value = 0
  319.             lblStatus.Text = "Completed!"
  320.             lblFile.Text = "Progress: 0%"
  321.             lblAll.Text = "Total compression: 0%"
  322.             MsgBox("Completed!", MsgBoxStyle.Information)
  323.         End If
  324.     End Sub
  325.     Private Sub strAddDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAddDir.Click
  326.         If lstFiles.SelectedNode Is Nothing Then
  327.             lstFiles.Nodes.Add(InputBox("Type the folder name:", "New folder")).ImageIndex = 0
  328.         Else
  329.             lstFiles.SelectedNode.Nodes.Add(InputBox("Type the folder name:", "New folder")).ImageIndex = 0
  330.         End If
  331.         strArk.Enabled = True
  332.     End Sub
  333.     Private Sub strAddRootDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAddRootDir.Click
  334.         lstFiles.Nodes.Add(InputBox("Type the folder name:", "New folder"))
  335.         strArk.Enabled = True
  336.     End Sub
  337.     Private Sub strImportDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strImportDir.Click
  338.         Dim F As New Windows.Forms.FolderBrowserDialog
  339.         F.Description = "Select the folder to import:"
  340.         If F.ShowDialog = Windows.Forms.DialogResult.OK Then
  341.             AddDirectory(F.SelectedPath, lstFiles.SelectedNode)
  342.             strArk.Enabled = True
  343.         End If
  344.     End Sub
  345.     Private Sub strOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strOpen.Click
  346.         Dim FOpen As New Windows.Forms.OpenFileDialog
  347.         FOpen.Filter = "Archive files|*.ark"
  348.         FOpen.FileName = "Archive"
  349.         If FOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
  350.             OpenArk(FOpen.FileName)
  351.         End If
  352.         strExtract.Enabled = True
  353.     End Sub
  354.     Private Sub strExtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExtract.Click
  355.         Dim F As New Windows.Forms.FolderBrowserDialog
  356.         F.Description = "Select the folder where to exctract files:"
  357.         If F.ShowDialog = Windows.Forms.DialogResult.OK Then
  358.             Try
  359.                 CopyDirectory(Application.StartupPath + "\Temp", F.SelectedPath + "\" + Path.GetFileName(lstFiles.Nodes(0).Text))
  360.                 MsgBox("Completed!", MsgBoxStyle.Information)
  361.             Catch IOE As IOException
  362.                 'MsgBox("E' stato riscontrato un errore durante l'estrazione delle cartelle. Probabilmente si è aperta " + _
  363.                 '"la cartella nella quale il file doveva essere estratto. Chiudere la cartella e riprovare.", MsgBoxStyle.Exclamation)
  364.                 MsgBox("An error has occurred while exctracting files. Probably you opened the folder in which files should have been placed. Close folder and try again.", MsgBoxStyle.Exclamation)
  365.                 Exit Sub
  366.             End Try
  367.         End If
  368.         lblStatus.Text = "Completed"
  369.         prgAll.Value = 0
  370.     End Sub
  371.     Private Sub strExtractSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EstraiElementoSelezionatoToolStripMenuItem.Click
  372.         If lstFiles.SelectedNode Is Nothing Then
  373.             MsgBox("No element selected!", MsgBoxStyle.Exclamation)
  374.             Exit Sub
  375.         End If
  376.         If lstFiles.SelectedNode.Nodes.Count = 0 And Path.HasExtension(lstFiles.SelectedNode.Tag) Then
  377.             Dim S As New Windows.Forms.SaveFileDialog
  378.             Dim N As TreeNode = lstFiles.SelectedNode
  379.             S.Filter = "File " + Path.GetExtension(N.Tag) + "|*" + Path.GetExtension(N.Tag)
  380.             If S.ShowDialog = Windows.Forms.DialogResult.OK Then
  381.                 FileCopy(lstFiles.SelectedNode.Tag, S.FileName)
  382.             End If
  383.         Else
  384.             Dim F As New Windows.Forms.FolderBrowserDialog
  385.             Dim N As TreeNode = lstFiles.SelectedNode
  386.             If F.ShowDialog = Windows.Forms.DialogResult.OK Then
  387.                 Dim S As String
  388.                 S = N.FullPath
  389.                 S = S.Remove(0, S.IndexOf("\") + 1)
  390.                 prgAll.Maximum = 100
  391.                 CopyDirectory(Application.StartupPath + "\Temp\" + S, F.SelectedPath + "\" + N.Text)
  392.             End If
  393.         End If
  394.         lblAll.Text = "Completed"
  395.         lblStatus.Text = "Completed"
  396.     End Sub
  397.     Private Sub frmArk_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  398.         strTools.Enabled = False
  399.         lblStatus.Text = "Deleting temporary files..."
  400.         If Directory.Exists(Application.StartupPath + "\Temp") Then
  401.             DeleteDirectory(Application.StartupPath + "\Temp")
  402.         End If
  403.     End Sub
  404.     Private Sub strBug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strBug.Click
  405.         MsgBox("Contact nicolo1990@yahoo.it.", MsgBoxStyle.Information)
  406.     End Sub
  407.     Private Sub strAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAbout.Click, strHelp.ButtonClick
  408.         Dim Bas As New AboutBox1
  409.         Bas.ShowDialog()
  410.     End Sub
  411. End Class