I'm trying to build a wp7 application that must allow user to read ebooks in epub format. Since there isn't any available library to read epub file on a windows phone, I'm trying to create one. So I must unzip the file and then parse it.
The problem is that I can't unzip the epub file. I'm using SharpZipLib.WindowsPhone7.dll but I get an exception:
Attempt to access the method failed: System.IO.File.OpenRead(System.String)
on this line:
ZipInputStream s = new ZipInputStream(File.OpenRead(path_epubfile));
Can any one help me, please?
It's going to depend on how the content is obtained. There's three possible options here;
Option 1: If the content is added to your project with a Build Action of "Content" you can obtain a stream by using the StreamResourceInfo class (In the System.Windows.Resources namespace)
StreamResourceInfo info = Application.GetResourceStream(new Uri("MyContent.txt", UriKind.Relative));
using (info.Stream) {
// Make use of the stream as you will
}
Option 2: If you've added it to your project and set the Build Action to "Embedded Resource" then you'll need to use GetManifestResourceStream()
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectName.MyContent.txt")) {
// Make use of stream as you will
}
Note: You'll need to replace "ProjectName" with the name of your project. So if your project was "EPubReader" and the embedded resource was "Example.txt" you'd need to pass "EPubReader.Example.txt" to GetManifestResourceStream(). You can use GetManifestResourceNames() to see what resources are available.
Option 3: If you've obtained the content at run time, it'll be stored in IsolatedStorage.
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) {
using (IsolatedStorageFileStream stream = store.OpenFile("MyContent.txt", FileMode.Open)) {
// Make use of stream as you will
}
}
Related
I have a class library that contains some generic proceesing functionality - call it "Engine".
I include the class library in a number of web applications.
The engine library needs an XML file as input, but the content is unique to each project.
At the moment I manually copy the XML file into each project. The engine always looks for a file in the application route.
However, I've gotten a little confused with regards to embedded resources. In order to validate the XML, I've created an XSD in my engine project and set the Build Action to EmbeddedResource.
I can't see the difference between setting the BuildAction to Content and EmbeddedResource in this case, which has led me to doubt the way that things are currently set up.
I've not a lot of experience at this level, so need some guidance. Any advice would be appreciated.
EmbeddedResource means that the xsd is embedded inside the assembly during build, while Content means it is just copied along to the output folder. You want the embedded resource thing it sounds like.
You can access Embedded resources through code like this:
string resourceName = "SomeNameSpace.SomeFile.xsd";
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if ( stream == null )
throw new ArgumentException("resource not found", "resourceName");
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
return result;
}
}
I'm having some really weird problems trying to load a file pragmatically into my project to save to a local folder.
I can load the file fine in my .xaml code, as so:
<BitmapImage x:Key="Image" UriSource ="/Assets/Submandibular_oedema.jpg" />
And I can display that image on my page. However, when I try to load the image and use it in my xaml.cs code like this
uriMyFile = new Uri("/Assets/Submandibular_oedema.jpg", UriKind.RelativeOrAbsolute);
It cannot find the file and as a result, won't let me do anything with the URI.
For background, my aim is to get the image stream then save it to a local folder.
I know it'll be a stupid little problem, but I can't find any solutions to it.
Thanks!
Try full path (specify your project assembly)
uriMyFile = new Uri("/YourAssemblyName;component/Assets/Submandibular_oedema.jpg",UriKind.RelativeOrAbsolute);
Update after Starktastics comment:
I finally understood your problem: you need the Application.GetResourceStream method to retrieve a stream of your resource, read from that stream and write it to a file stream.
var streamResourceInfo = Application.GetResourceStream(uri);
var stream = streamResourceInfo.Stream;
var byteBuffer = new byte[stream.Length];
using (stream)
{
stream.Read(byteBuffer, 0, byteBuffer.Length);
}
using (var fileStream = new FileStream("photo.jpg", FileMode.Create))
{
fileStream.Write(byteBuffer, 0, byteBuffer.Length);
}
I've updated the solution that you can download here.
Original answer:
If you use a WPF application, make sure that the asset you added to your project is to Build Action 'Resource'. You can check that in the properties pane after you clicked the file.
In any case, your syntax for the URI is correct. I checked it in a small project myself and it works. You can download it here (DropBox link)
Feel free to leave a comment.
Try to use
uriMyFile=new Uri("ms-appx:///Assets/YourImage.png"));
This works for me while trying to show a diffrent map icon for each place.
i am trying to make a simple application in which i have an image i have copied it in Assets folder of my project. The image i got from the web, and it is in the png format.
can some body give me an idea that how i can copy my images to my project so that when i deploy the project on device i will able to load them.
Current i what i am trying is.
var streamResource = App.GetResourceStream(new Uri("/Assets/Tiles/gradiant-mask.png", UriKind.Relative));
using (Stream stream = streamResource.Stream) {
var maskData = new byte[stream.Length];
stream.Read(maskData, 0, maskData.Length);
}
But i always get the streamResource object as null and the may be the reason is it didn't find the file on the device. can some body guide me that how i can load the image on the device in my wp8 application.
Make sure the Build action is set to Content on the properties of the image file in Visual Studio.
If you want the Build action to be set to Resource, use the following URI syntax:
new Uri("/YOUR_PROJECT_NAME;component/Assets/Tiles/gradiant-mask.png", UriKind.Relative)
Using the Content build action is recommended.
you can directly load image by :
in XAML
Source="/Assets/Tiles/gradiant-mask.png"
or in code behind by
imagename.Source = new Uri("/Assets/Tiles/gradiant-mask.png",UriKind.Relative);
set the build action as content .
I am doing Silverlight application. which i need to save data into server.
Is it Possible to Save recorded stream in one dummy file.
Stream stream = saveFileDialog.OpenFile();
WavManager.SavePcmToWav(_sink.BackingStream, stream, _sink.CurrentFormat);
stream.Close();
Instead of selecting user by SaveFileDialog I want to use Dummy file at runtime.
if it possible any one will tell i will greatly appreciate.Advance thanks.
You can use the IsolatedStorageFile to create a temp/dummy file without asking the user to select a file.
The IsolatedStorage is a restricted area for your silverlight application to store files and data.
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication ();
IsolatedStorageFileStream stream = store.CreateFile("dummy.wav");
WavManager.SavePcmToWav(_sink.BackingStream, stream, _sink.CurrentFormat);
stream.Close();
Another solution would be to store the data of your .wav file in a in-memory stream. This can be done by using a MemoryStream.
I am using icsharpziplib dll for zipping sharepoint files using c# in asp.net
When i open the output.zip file, it is showing "zip file is either corrupted or damaged".
And the crc value for files in the output.zip is showing as 000000.
How do we calculate or configure crc value using icsharpziplib dll?
Can any one have the good example how to do zipping using memorystreams?
it seems you're not creating each ZipEntry.
Here's is a code that I adapted to my needs:
http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx#Create_a_Zip_fromto_a_memory_stream_or_byte_array_1
Anyway with SharpZipLib there are many ways you can work with zip file: the ZipFile class, the ZipOutputStream and the FastZip.
I'm using the ZipOutputStream to create an in-memory ZIP file, adding in-memory streams to it and finally flushing to disk, and it's working quite good. Why ZipOutputStream? Because it's the only choice available if you want to specify a compression level and use Streams.
Good luck :)
1:
You could do it manually but the ICSharpCode library will take care of it for you. Also something I've discovered: 'zip file is either corrupted or damaged' can also be a result of not adding your zip entry name correctly (such as an entry that sits in a chain of subfolders).
2:
I solved this problem by creating a compressionHelper utility. I had to dynamically compose and return zip files. Temp files were not an option as the process was to be run by a webservice.
The trick with this was a BeginZip(), AddEntry() and EndZip() methods (because I made it into a utility to be invoked. You could just use the code directly if need be).
Something I've excluded from the example are checks for initialization (like calling EndZip() first by mistake) and proper disposal code (best to implement IDisposable and close your zipfileStream and your memoryStream if applicable).
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
public void BeginZipUpdate()
{
_memoryStream = new MemoryStream(200);
_zipOutputStream = new ZipOutputStream(_memoryStream);
}
public void EndZipUpdate()
{
_zipOutputStream.Finish();
_zipOutputStream.Close();
_zipOutputStream = null;
}
//Entry name could be 'somefile.txt' or 'Assemblies\MyAssembly.dll' to indicate a folder.
//Unsure where you'd be getting your file, I'm reading the data from the database.
public void AddEntry(string entryName, byte[] bytes)
{
ZipEntry entry = new ZipEntry(entryName);
entry.DateTime = DateTime.Now;
entry.Size = bytes.Length;
_zipOutputStream.PutNextEntry(entry);
_zipOutputStream.Write(bytes, 0, bytes.Length);
_zipOutputStreamEntries.Add(entryName);
}
So you're actually having the zipOutputStream write to a memoryStream. Then once _zipOutputStream is closed, you can return the contents of the memoryStream.
public byte[] GetResultingZipFile()
{
_zipOutputStream.Finish();
_zipOutputStream.Close();
_zipOutputStream = null;
return _memoryStream.ToArray();
}
Just be aware of how much you want to add to a zipfile (delay in process/IO/timeouts etc).