Unzipping a File in vb.net - vb.net

I have an embedded resource in my .exe that is a zip file, I would like to move it out of the resources and unzip it to a specific folder.
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btn_Install.Click
Dim Dir_File As String = "C:\FTB"
Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
Dim directorySelected As DirectoryInfo = New DirectoryInfo(Dir_Temp)
End Sub
But I have no way of extracting the the .zip file to a directory. So all I need now is a way to actual extract the .zip.
I tried this:
Dim directorySelected As DirectoryInfo = New DirectoryInfo(Dir_Temp)
For Each fileToDecompress As FileInfo In directorySelected.GetFiles("Unleashed.zip")
Using OriginalFileStream As FileStream = fileToDecompress.OpenRead()
Using decompressedFileStream As FileStream = File.Create(Dir_File & "\Unleashed")
Using decompressionStream As GZipStream = New GZipStream(OriginalFileStream, CompressionMode.Decompress)
decompressionStream.CopyTo(decompressedFileStream)
End Using
End Using
End Using
Next
But all that did was give me an error about a magic number.
Any help is very much appreciated.

Your problem is allready discussed here: https://stackoverflow.com/a/11273427/2655508
The GZipStream class is not suitable to read compressed archives in the .gz or .zip format. It only knows how to de/compress data, it doesn't know anything about the archive file structure. Which can contain multiple files, note how the class doesn't have any way to select the specific file in the archive you want to decompress.
The solution is to use NET 4.5 which contains inside the System.IO.Compression namespace the needed classes with their methods for creating, manipulating and extracting items in/out of a zip file.
See e.g. System.IO.Compression.ZipFileExtensions.ExtractToDirectory()

Gzip is different from zip. The file format is identified by the 2 to 4 first bytes of the file:
Zip: 50 4b 03 04
Gzip: 1f 8b
Since you are trying to decompress a zip file with gzip you get an error message essencially telling you that your file is not a gzip file.
See
Unzip files programmatically in .net for similar question.
For magic number see: http://en.m.wikipedia.org/wiki/Magic_number_(programming)

If you can use the NET 4.5 it ships inside the System.IO.Compression namespace and all the stuff needed for zip file manipulation.
For zip manipulation you can consider using a third party library like sharpziplib I use it with success in many projects.
Take a look to these samples: https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples

Related

How to add disk in zip using DotNetZip

I use DotNetZip for creating zips. It has many option but I couldn't find if it is possible to store the disk where the file is located, in the archive. E.g. like the Absolute mode in 7-Zip. As far as I can see I can only do this:
zip.AddFile(cFileFull, cPath);
When cFileFull is e.g. "c:\temp\SomeFile.txt" and cPath = "c:\temp" opening the zipfile shows
temp
while I would like to see
C
and then, when I click on C
temp
This allows storing the same path/file found on different drives. Is this possible?

How does numpy handle mmap's over npz files?

I have a case where I would like to open a compressed numpy file using mmap mode, but can't seem to find any documentation about how it will work under the covers. For example, will it decompress the archive in memory and then mmap it? Will it decompress on the fly?
The documentation is absent for that configuration.
The short answer, based on looking at the code, is that archiving and compression, whether using np.savez or gzip, is not compatible with accessing files in mmap_mode. It's not just a matter of how it is done, but whether it can be done at all.
Relevant bits in the np.load function
elif isinstance(file, gzip.GzipFile):
fid = seek_gzip_factory(file)
...
if magic.startswith(_ZIP_PREFIX):
# zip-file (assume .npz)
# Transfer file ownership to NpzFile
tmp = own_fid
own_fid = False
return NpzFile(fid, own_fid=tmp)
...
if mmap_mode:
return format.open_memmap(file, mode=mmap_mode)
Look at np.lib.npyio.NpzFile. An npz file is a ZIP archive of .npy files. It loads a dictionary(like) object, and only loads the individual variables (arrays) when you access them (e.g. obj[key]). There's no provision in its code for opening those individual files inmmap_mode`.
It's pretty obvious that a file created with np.savez cannot be accessed as mmap. The ZIP archiving and compression is not the same as the gzip compression addressed earlier in the np.load.
But what of a single array saved with np.save and then gzipped? Note that format.open_memmap is called with file, not fid (which might be a gzip file).
More details on open_memmap in np.lib.npyio.format. Its first test is that file must be a string, not an existing file fid. It ends up delegating the work to np.memmap. I don't see any provision in that function for gzip.

How to modify .INI file with using sharpconfig?

I am using sharpconfig for Load my .INI file. i got success in reading ini file and its very user friendly.
Dim config As New SharpConfig.Configuration
config = SharpConfig.Configuration.Load("D:\Myini.ini")
Now i want to replace particular word of .ini file. SharpConfig showing that its also providing the .INI file modify functionality but i can not able to find how i can modify my file with sharpconfig
Please help me. Thanks!
Given an INI file that looks like this:
[MySection]
MySetting = 123
You read it with SharpConfig.Configuration.Load:
Dim yourpath = "c:\WhatEver.ini"
Dim config = SharpConfig.Configuration.Load(yourpath)
Console.WriteLine(config("MySection")("MySetting").Value)
This code will print
123
Now, to change the INI file, simply assign a new value and save it like:
config("MySection")("MySetting").Value = "Foobar"
config.Save(yourpath)
The INI file will now look like this:
[MySection]
MySetting = Foobar
You already refered to the codeplex page and there you can even find a a test app that shows how to do stuff with it: TestApp
If you have the Category and the Setting you can get/set the value of the Setting with .Value
In the Sourcecode of SharpConfig it is easy readable what can be accessed. I'm a VB guy myself but it should be no problem to read the c# stuff >> Settings

Filter files from directory vb.net

Straight to the question...I have files such as word documents with extension(.doc) and its respective sample files starting with (.sample)
Now I would like to load only the word documents..
I found the way as shown below to load the files but this loads all the files
Can anyone say me how do I filter these files while loading them ?
This is what I'm trying to do:
Dim files = Array.FindAll(Directory.GetFiles(mydir), Function(x) (Not x.StartsWith(".sample")))
This is my directory consists of files as said above:
The way you use it, all the files are retrieved (paying the whole computational cost) and then they are filtered.
As stated in this article, you can use a search pattern directly in file retrieval from your file system.
I suppose you could do something like that:
Dim files = Directory.GetFiles(mydir,".doc*")
If you gave an example of filenames, perhaps I would give you the right filter to apply too.
Hope I helped!
The GetFiles method returns filenames with the path that you specified included.
So if your files are in a folder C:\working\, your mydir variable will contain "C:\working\" and all of the results of GetFiles will be something like
"C:\working\.sample_filename.doc"
"C:\working\123797.doc"
So your x.StartsWith is always going to return false, because x always starts with C:\
Try this:
Dim files = Array.FindAll(Directory.GetFiles(mydir), Function(x) (Not x.StartsWith(mydir & ".sample")))
Note this assumes that your mydir variable ends with a \ character. If not, add it in in the concatenation within the function.
Try this,
Dim files = Array.FindAll(Directory.GetFiles(mydir), Function(x) (Not Path.GetFileName(x).StartsWith(".sample")))

How do i create a "valid" unix TAR Archive in Windows with Java (apache compress)?

I'm trying to create a 'valid' tar.gz archive by using the apache commons compress librarys. The created archive will be read by an embedded device and has to be in the same format with the same file permissions i think.
If i'm using Linux to create my file, everything works fine, but if i'm using Windows. The file is rejected.
As you can see, the archive only contains to special files with unix permissions. these are correctly set and if i use a "working" tar file and run it through gzip, the created tar.gz also works fine.
The only difference i figured out is, that the non-working tar file is slightly larger (61 instead of 56 kb) and 7zip shows under "Host OS" FAT instead of Unix.
Any ideas, how i can create a "real" tar archive from Windows?
Thanks in advance!
My current sourcecode is:
public static void compress(File configTar, File rcConf, File databaseTxt)
throws ArchiveException, IOException {
OutputStream tarFileStream = new GZIPOutputStream(new FileOutputStream(configTar));
InputStream rcConfStream = new FileInputStream(rcConf);
InputStream databaseTxtStream = new FileInputStream(databaseTxt);
ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory()
.createArchiveOutputStream(ArchiveStreamFactory.TAR, tarFileStream);
TarArchiveEntry databaseTxtEntry = new TarArchiveEntry(databaseTxt);
TarArchiveEntry rcConfEntry = new TarArchiveEntry(rcConf);
databaseTxtEntry.setName("database.txt");
databaseTxtEntry.setGroupName("root");
databaseTxtEntry.setUserName("root");
databaseTxtEntry.setMode(convertModeFromString("rwxr-xr-x"));
archiveOutputStream.putArchiveEntry(databaseTxtEntry);
IOUtils.copy(databaseTxtStream, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
rcConfEntry.setName("rc.conf");
rcConfEntry.setGroupName("root");
rcConfEntry.setUserName("root");
rcConfEntry.setMode(convertModeFromString("rw-rw-rw-"));
archiveOutputStream.putArchiveEntry(rcConfEntry);
IOUtils.copy(rcConfStream, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
archiveOutputStream.finish();
rcConfStream.close();
databaseTxtStream.close();
tarFileStream.close();
}
I've done some research and noticed a difference between the TAR headers. Can anybody tell, what i am doing wrong?
Working example of file 1:
http://i.stack.imgur.com/S8Rbi.jpg
NON-Working example of file 1:
http://i.stack.imgur.com/bdc9T.jpg
Working example of file 2:
http://i.stack.imgur.com/rYhr9.jpg
NON-Working example of file 2:
http://i.stack.imgur.com/4wHw3.jpg
You need to use different constructor. I you check JavaCode TarArchiveEntry
new TarArchiveEntry(file) will end up into new TarArchiveEntry(file, file.getPath())
so, if you use new TarArchiveEntry(file, file.getName()) that will make it flat