Wednesday, 15 June 2011

vb.net - Compression ZipArchive won't compile -



vb.net - Compression ZipArchive won't compile -

i have class, i've converted c# vb.net (framework 4.5)

i'm trying utilize new compression features. think i've followed instructions msdn it's still not compiling.

zipfile, ziparchive , ziparchiveentry coming "type not defined"

i've imported namespace "system", "system.io" , "system.io.compression" in references

what else have work?

imports scheme imports system.io imports system.io.compression imports system.collections.generic imports system.linq public class clszip1 ' used specify our overwrite policy files extracting. public enum overwrite = 0 ifnewer = 1 never = -1 end enum ' used identify if trying create zip file , exists. public enum archiveaction emerge = 0 ereplace = 1 eerror = 2 eignore = 3 end enum ' unzips specified file given folder in safe manner. ' plans missing paths , existing files , handles them gracefully. public sub extracttodirectory(byval sourcearchivefilename string, byval destinationdirectoryname string, optional byval overwritemethod overwrite = overwrite.ifnewer) 'opens zip file read using archive ziparchive = zipfile.openread(sourcearchivefilename) 'loops through each file in zip file each file ziparchiveentry in archive.entries improvedextracttofile(file, destinationdirectoryname, overwritemethod) next end using end sub ' safely extracts single file zip file public sub extracttofile(byval file ziparchiveentry, byval destinationpath string, optional byval overwritemethod overwrite = overwrite.ifnewer) 'gets finish path destination file, including 'relative paths in zip file dim destinationfilename string = path.combine(destinationpath, file.fullname) 'gets new path, minus file name can create 'directory if not exist dim destinationfilepath string = path.getdirectoryname(destinationfilename) 'creates directory (if doesn't exist) new path directory.createdirectory(destinationfilepath) 'determines file based upon 'method of overwriting chosen select case overwritemethod case overwrite.always 'just set file in , overwrite found file.extracttofile(destinationfilename, true) case overwrite.ifnewer 'checks see if file exists, , if so, if should 'be overwritten if (not file.exists(destinationfilename) or file.getlastwritetime(destinationfilename) < file.lastwritetime) 'either file didn't exist or file newer, 'we extract , overwrite existing file file.extracttofile(destinationfilename, true) end if case overwrite.never 'put file in if new ignores 'file if exists if (not file.exists(destinationfilename)) file.extracttofile(destinationfilename) end if end select end sub ' allows add together files archive, whether archive exists or not public sub addtoarchive(byval archivefullname string, byval files list(of string), optional byval action archiveaction = archiveaction.ereplace, optional byval fileoverwrite overwrite = overwrite.ifnewer, optional byval compression compressionlevel = compressionlevel.optimal) 'identifies mode using - default create dim mode ziparchivemode = ziparchivemode.create 'determines if zip file exists dim archiveexists boolean = file.exists(archivefullname) 'figures out based upon our specified overwrite method select case action case archiveaction.emerge 'sets mode update if file exists, otherwise 'the default of create fine if (archiveexists) mode = ziparchivemode.update case archiveaction.ereplace 'deletes file if exists. either way, default 'mode of create fine if (archiveexists) file.delete(archivefullname) case archiveaction.eerror 'throws error if file exists if (archiveexists) throw new ioexception(string.format("the zip file {0} exists.", archivefullname)) case archiveaction.eignore 'closes method silently , nil if (archiveexists) homecoming case else end select 'opens zip file in mode specified using zipfile ziparchive = zipfile.open(archivefullname, mode) 'this bit of hack , should refactored - 'doing similar foreach loop both modes, create 'i doing little work while update gets lot of 'code. not handle other mode (of 'which there wouldn't 1 since don't 'use read here). if (mode = ziparchivemode.create) each file string in files 'adds file archive zipfile.createentryfromfile(file, path.getfilename(file), compression) next else each file string in files dim fileinzip = (from f in zipfile.entries f.name = path.getfilename(file) select f).firstordefault() select case fileoverwrite case overwrite.always 'deletes file if found if (fileinzip isnot nothing) fileinzip.delete() end if 'adds file archive zipfile.createentryfromfile(file, path.getfilename(file), compression) case overwrite.ifnewer 'this bit trickier - delete file if 'newer, if newer or if file isn't in 'the zip file, write zip file if (fileinzip isnot nothing) 'deletes file if older our file. 'note file ignored if existing file 'in archive newer. if (fileinzip.lastwritetime < file.getlastwritetime(file)) fileinzip.delete() 'adds file archive zipfile.createentryfromfile(file, path.getfilename(file), compression) else 'the file wasn't in zip file add together archive zipfile.createentryfromfile(file, path.getfilename(file), compression) end if end if case overwrite.never 'don't - decision need 'consider, however, since mean no file 'be writte. write sec re-create zip 'the same name (not sure wise, however). case else end select next end if end using end sub end class

you need add together reference system.io.compression.dll. have vs2010 @ work, can't test this, i'm guessing it's available framework 4.5, since it's not on pc.

open 'my project', go references tab, click 'add...' drop-down , select 'reference'. in add together reference dialog, utilize .net tag, sort list component name , find system.io.compression.

if it's not there, seek searching hard drive dll , utilize same reference dialog add together manually. who's got experience particular library can clarify abnormalities in using it.

vb.net compression .net-4.5 ziparchive

No comments:

Post a Comment