Check if a file is complete after downloading in Objective-c - iphone

I am implementing resumable file downloading for the iPhone, and I need to know how I can tell whether a file I have previously downloaded is complete or not before I attempt to redownload.

I've decided to use a '.part' or similar extension for files that are in progress of being downloaded. Then once the -(void)connectionDidFinishLoading:(NSURLConnection *)connection method executes, I will rename it.

You'll need to store the information about the size of the complete file somewhere that can be matched up with the partially downloaded file -- or, in the least, something that indicates the file is partially downloaded.
When your app wants to do anything with any of the downloaded files, it needs to check for partially downloaded content (i.e. actual size of data you have < complete file size) -- if it's only downloaded partially, it needs to kick off a resume download.
Without more specific info, can't say much more than that!

Related

File Uploading in Sakai

I want to know if there is a 'right' way to make file uploads through custom tools.
I've seen the https://confluence.sakaiproject.org/display/BOOT/File+Uploads+with+RSF guide and it seens ok, but It stops with the file in memory with no further info. I can built a random file upload code but I want to make it Sakai-friendly (Using ContentHosting and Resources service?)
Any hints?
Thanks
The link you provided for the first part is a good example of how to get the upload initially processed. Going through RequestFilter will get your files validated, but you can use whatever method you want to upload it.
For the second part, I'd look at the ContentHosting webservice (createContentItem) for an example of how to add a file from a byte[] in memory after you've uploaded it.
These methods in ContentHostingService also accept InputStream as a parameter as of 2.7 (KNL-325), so you don't have to store the entire file in memory and can stream it as you're uploading, which you should do if the files are of any reasonable size.

Write a File into the App Bundle

I have a Webservice to download some News and write them into a plist-file somewhere in NSLibraryDirectory, thats fine. But if the user has no Internet connection or the webservice is offline or whatever, I load a Default-Newsfile from within the AppBundle.
At the moment I have to replace the Default-File manually before every AppStoreUpdate to keep it up to date.
My Question: Is there an easy way to write into the App Bundle while debugging via Simulator, so the Default-File will everytime be up to date.
I tought about something like:
#ifdef SIMULATOR
//Write to AppBundle
#endif
Note:
It's all about the time while I use the Mac and the Simulator, I don't want to do that in the Published App!
Yes, I already replace the Ressource in my Bundle, but by hand and I want it to realize automatically.
I want to keep my Projectfolder up to date, before I compile it for any Device or the AppStore.
AppBundle is Read-only you cannot write anything to it programmatically, however if you update the resources in your Project through Xcode, you get the updated file automatically in the AppBundle..
Ok, based on your comment, here's what you need (I think - still not completely clear on the question, but let me know if this works for you).
Create an empty file (say news.plist) in your project folder (SRCROOT). Add this to your repo.
Add this file in your project's resources and add it in Copy Bundle Resources step in Build Phases
When you download content from internet, save it to project's temporary folder, then copy it to SRCROOT/news.plist overwriting existing file (optionally add a check on file checksum to avoid unnecessary change). This step is required ONLY when running on simulator.
Whenever developers check-in, the updated news.plist should be checked in to the repo.
To make SRCROOT available in code, add SRCROOT=\"${SRCROOT}\" to GCC_PREPROCESSOR_DEFINITIONS.
Other developer won't need to do anything except the last step - check in the updated file every time it changes.
Does this help?
No, you might be possible to get the simulator to write to you App Bundle, but this does not mean that the file your project (the one that gets compiled when you build for the app store) will be updated.
Why not just save the new to the documents directory, then always load this file. After the app is started, start a background thread and try to update the plist file. Of you are successful in retrieving the news over the write the plist file in the document directory.
This way the user will have an file that was retrieved the last time the device was able to get the file.
You could add an file in the app bundle which you copy to the documents directory on first start of the application, just to make sure that the user has data in the app.

How to verify the integrity of a downloaded file?

StoreKit has a bug that can lead to incomplete downloads being marked as "Finished".
I have seen some apps that download large files after In-App Purchases do an integrity check on them. How is this done?
Something I do in my own app is to pass a JSON with a list of resources will be downloading. Beside passing the URL of the resource, I also pass the checksum of it. After the download for a given file I compared the checksum I have from the JSON with the checksum of the file itself. You can use something like this.

Download and unpack a *.tgz file to a subdirectory of the Documents directory

There very well may be an answer to this already on SO, but I'm not familiar enough with compression formats to know if they're applicable to my case. So here's what I need:
1) Download a *.tgz file that is greater than 200MB.
2) Unpack it to a specified subdirectory of the Documents folder.
I know how to make the connection and begin downloading. But how do I download to an actual file (rather than storing it in memory), and once this download is complete how do I unpack it to my desired location?
To save the downloaded data to a file, see this SO-question and answer(s): The easiest way to write NSData to a file
To uncompress .tgz-files, see this question and answer(s): "Untar" file on iPhone
To download large files, see this question and answer(s): How to download large files using objective c on iphone
(Google is an awesome tool, really.)
Just as a sidenote, an app shouldn't download 200MB of data. It is time- and bandwith consuming and may cause Apple to reject your app.
Forget NSURLConnection; use ASIHTTPConnection (google it) which has an easy save to file option. (And resumes failed downloads too)
I don't know the answer to tar/gzip. My application uses zips instead and http://code.google.com/p/ziparchive/wiki/PageName does the trick.

download multiple files using SimpleFTPSample

I've been trying to figure out how to download multiple files in a row based on the SimpleFTPSample provided by apple. Basically, I'm filtering what the user can see when they browse an ftp server, but when they select a certain file type, I want it to automatically check for another file of the same name with a different extension and if it exists, download it as well. I can't seem to get this second file to download no matter what I do. It seems strange because if I select two files in a row in my tableview, it downloads both of them just fine. Any ideas?
Edit:
It's just the SimpleFTPSample from apple.developer.com, all I did was create additional NSInputStream and NSOutputStream objects and I created a new _startReceiveFile method that gets called from _startReceive if I'm downloading a file instead of getting a directory listing. _startReceiveFile is the same code for _startReceive in the file download code for the sample project, except if the file to download has a certain extension, it also downloads an additional file with the additional stream objects. Let me know if I need to clarify more or try to put together a clear example.
Well, since there were no takers, I'll just post my solution here. I've abandoned trying to download two files at once. Instead, I just keep the ftp browsing window open and only handle the opening of the file once both files have been downloaded (user has clicked on each one separately). It's not what I wanted, but it will work, at least until I can figure out how to get two files with one request.