2008年11月21日 ..:: Blog ::.. 注册  登录
 导航

 打印   
 搜索


 打印   
 博客列表

 打印   
 友情链接

 打印   
 校友录

 打印   
 How to use VB.Net, Java Compress a Folder into ZIP file
地区 Blogs-=FengLiN=-My Work    
作者: FengLiN 9/10/2007 8:00 AM
I wrote this simple program is for my daily backup purpose. With this program I can compress the folder into a zip file and save to my backup folder.

1. Add reference: .NET ---> VJSLIB

2. Imports NameSpace:

Imports java.util.zip
Imports java.util
Imports java.io

3. Call Java function to Compress files and extract zip file

A: Compress to Zip File

Public Function ZipFiles(ByVal zipFileName As String, ByVal sourceRootPath As String, ByVal sourceFiles As System.Collections.ArrayList) As Boolean
        ZipFiles = False
        Try
            Dim objFileOutStream As New FileOutputStream(zipFileName)
            Dim objZipOutStream As New ZipOutputStream(objFileOutStream)
            Dim objFileInStream As FileInputStream = Nothing
            Dim ze As ZipEntry
            Dim len As Integer
            Dim fileCount = sourceFiles.Count - 1
            Dim fileName As String
            For i As Integer = 0 To fileCount
                fileName = sourceFiles(i)
                objFileInStream = New FileInputStream(fileName)
                ze = New ZipEntry(fileName.Replace(sourceRootPath, String.Empty).Substring(1))
                objZipOutStream.putNextEntry(ze)
                Dim buffer(1024) As SByte
                len = 0
                len = objFileInStream.read(buffer)
                While len >= 0
                    objZipOutStream.write(buffer, 0, len)
                    len = objFileInStream.read(buffer)
                End While
            Next
            objZipOutStream.closeEntry()
            objFileInStream.close()
            objZipOutStream.close()
            objFileOutStream.close()
            ZipFiles = True
        Catch ex As Exception
            Me._expObj = ex
        End Try
    End Function

 B: Extract ZIp File

Public Function Extract(ByVal sourceZipFile As String, ByVal destinationPath As String) As Boolean
        Extract = False
        Try
            Dim zipfile As New ZipFile(sourceZipFile)
            Dim zeList As List(Of ZipEntry) = Me.GetAllZipEntries(zipfile)
            Dim dirPath As String
            Dim len As Integer
            For Each ze As ZipEntry In zeList
                If Not ze.isDirectory Then
                    Dim s As InputStream = zipfile.getInputStream(ze)
                    Try
                        dirPath = Path.Combine(destinationPath, Path.GetDirectoryName(ze.getName))
                        System.IO.Directory.CreateDirectory(dirPath)
                        Dim dest As New FileOutputStream(Path.Combine(dirPath, Path.GetFileName(ze.getName)))
                        Try
                            len = 0
                            Dim buffer(7168) As SByte
                            len = s.read(buffer)
                            While len >= 0
                                dest.write(buffer, 0, len)
                                len = s.read(buffer)
                            End While
                        Catch ex As Exception
                            Me._expObj = ex
                            Exit Function
                        Finally
                            dest.close()
                        End Try
                    Catch ex As Exception
                        Me._expObj = ex
                        Exit Function
                    Finally
                        s.close()
                    End Try
                End If
            Next
            Extract = True
        Catch ex As Exception
            Me._expObj = ex
        End Try
    End Function

Download Source Code and Demo Project : Password: www.hczhu.com

Copyright © 2007 FengLiN (www.hczhu.com)
固定链接 |  引用

您的名字:
标题:
评论:
验证码
在下面的文本框中输入上面图片中的验证码
添加评论   取消 

  
 推荐

 打印   
 存档

 打印   
 支持我们

 打印   
Copyright 2008 by 三片叶   服务条款  隐私声明