I have a program I made in vb.net express 2008 that used java.utils.zip to compress my work files to a Zip archive that I ran occassionaly to have a backup. I worked fine, but with net framework 4 and vb.net 2010 it doesn't work. So I modified it to use sharpziplib and it works fine if all files on the filter list in the folder are closed, but if I have a file open in OpenOffice writer, my program can't access it and won't create the zip file. The file that usually stays open all the time is my TODO list, that is updated several times a day , so usually gets printed, and updated( adding or deleting entries), without closing it.
Is there a way to read a file or to load a copy into memory and process it from there without closing it in the other application?
Here it the module:
Imports System
Imports System.IO
Imports ICSharpCode.SharpZipLib.Core
Imports ICSharpCode.SharpZipLib.Zip
Module ZipClass
Public Sub CreateSample(ByVal outPathname As String, ByVal password As String, ByVal folderName As String)
Dim ExtensionsToCompress As String = ".TXT_.REC_.REP_.ODS_.ODT_.DOC_.XLS_.RTF"
Dim Filter As String
Dim fsOut As FileStream = File.Create(outPathname)
Dim zipStream As New ZipOutputStream(fsOut)
zipStream.SetLevel(3) '0-9, 9 being the highest level of compression
zipStream.Password = password ' optional. Null is the same as not setting.
Dim files As String() = Directory.GetFiles(folderName)
For Each filename As String In files
Dim fi As New FileInfo(filename)
Filter = UCase(Path.GetExtension(filename))
If filename.Contains(".zip") Then ' <> Form1.ThePath & String.Format("{0:MMMddyy}", DateTime.Now) & ".zip" Then
Else
If ExtensionsToCompress.Contains(Filter) Then
Dim entryName As String = ZipEntry.CleanName(filename) ' Removes drive from name and fixes slash direction
Dim newentry As New ZipEntry(Path.GetFileName(filename)) '' No Path, Only FileName
newentry.DateTime = fi.LastWriteTime ' Note the zip format stores 2 second granularity
' Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
' newEntry.AESKeySize = 256;
' To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
' you need to do one of the following: Specify UseZip64.Off, or set the Size.
' If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
' but the zip will be in Zip64 format which not all utilities can understand.
' zipStream.UseZip64 = UseZip64.Off;
newentry.Size = fi.Length
zipStream.PutNextEntry(newentry)
' Zip the file in buffered chunks
' the "using" will close the stream even if an exception occurs
Dim buffer As Byte() = New Byte(4095) {}
Using streamReader As FileStream = File.OpenRead(filename)
StreamUtils.Copy(streamReader, zipStream, buffer)
End Using
zipStream.CloseEntry()
End If
End If
Next
zipStream.IsStreamOwner = True ' Makes the Close also Close the underlying stream
zipStream.Close()
End Sub
End Module
Any help will be greatly appreciated
ricardosms

New Topic/Question
Reply



MultiQuote






|