Add multiple files to a Zip file using DotNetZip is too slow - dotnetzip

I followed the example below: (https://dotnetzip.codeplex.com)
String[] filenames = { "ReadMe.txt", "c:\\data\\collection.csv", "c:\\reports\\AnnualSummary.pdf"};
using (ZipFile zip = new ZipFile())
{
zip.CompressionLevel = CompressionLevel.None;
zip.AddFiles(filenames, "files");
zip.Save("Archive.zip");
}
But when I add 200 audio files to a zip, it too slow, even copy is faster.
How can I add multiple file to zip is faster ? Another solution or work around ?

Related

Editing file in zip using Truezip is creating a tmp file, but not editing the file inside zip

I want to edit a file "application.properties" inside "imp" directory inside a zip "archive.zip". I'm using Truezip for this. I tried using both TFileOutputStream and TFileWriter. But both of them are creating a tmp file, but not editing the actual file in zip. The actual file in zip still remains same. Below are my code samples:
1. Using TFileOutputStream:
OutputStream out = new TFileOutputStream("C:\sample\archive.zip\imp\application.properties");
// Loading properties code here
properties.store(out, null);
out.close();
Using TFileWriter:
File entry = new TFile("C:\sample\archive.zip\imp\application.properties");
Writer writer = new TFileWriter(entry);
try {
writer.write("wish.world=Hello world\n");
} finally {
writer.close();
}
Kindly help.
You need to call TVFS.umount() to persist your changes.

Stream pdfs from url and add it to Zip

I have a mvc 4.5 application where I show a grid. The first column of the grid is a document name. The document name is an hyper link to the actual document that is hosted on our site and is available via a url. The documents can be pdf or doc or ppt. I can access these documents only via url and I do not have access to the actual physical document on our server.
I am providing users an option to select one or many of these documents from the grid and then they can download them. What I am trying to achieve is read each of the selected documents via the url and write it to a zip file and make the zip file downloadable. So users will be downloading one file instead of multiple files.
I have tried to stream the documents via url in memory and then add it to the zip file using ZipArchive Library from Microsoft. This is not working for me.
I was able to add documents that was on disk to zip file using Zip Archive and it works great. But I do not have access to the physical document as I can access the documents only through URL. My next option is to download each of these documents into a temp location on server and then add it to zip file using Zip Archive.But I am trying to avoid downloading files into a temp location
Please suggest how I can achieve reading documents via url in memory and adding each of these document to zip file and make zip file downloadable.
Any help will be appreciated.
Thank you Cbroe for commenting. I figured the answer. The problem was I was reading the pdf from the url and convert it to a memory stream and then was trying to add the memory stream to ZipArchive which was not working but instead I extracted the byte array out of the memory stream and then added it to the zip archive and it worked.
Here is the code snippet that might be useful for some one. My first contribution to Stack OverFlow.
public FileResult DownloadZip()
{
MemoryStream memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var demoFile = archive.CreateEntry("Pdf123.pdf");
var convertedStream = ConvertTobyte("http://www.example.com/Pdf123.pdf");
using (var entryStream = demoFile.Open())
{
entryStream.Write(convertedStream, 0, convertedStream.Length);
}
demoFile = archive.CreateEntry("Pdf456.pdf");
convertedStream = ConvertTobyte("http://www.example.com/Pdf456.pdf");
using (var entryStream = demoFile.Open())
{
entryStream.Write(convertedStream, 0, convertedStream.Length);
}
}
//This option is to write the zip to your local disk
using (var fileStream = new FileStream(#"C:\Temp\test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
//This option is to donload the zip via browser
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/zip")
{
FileDownloadName = "Archive.zip"
};
}
private static byte[] ConvertTobyte(string fileUrl)
{
byte[] imageData = null;
using (var wc = new System.Net.WebClient())
imageData = wc.DownloadData(fileUrl);
return imageData;
}

Need a more efficent way to merge text files

This is my code at present
Dim Paths() As String = Directory.GetFiles("files*.txt")
For Each Path As String In Paths
File.AppendAllText("merged.txt", File.ReadAllText(Path), Encoding.Default)
Next
The problem seems that using this method, performance is poort when dealing with several large files.
Is there a more efficent way to merge text files? Maybe reading all the files into a streamreader first and then creating the output file in one operation?
try this:
using (StreamWriter sw = new StreamWriter("merge.txt"))
{
string[] paths = Directory.GetFiles("files*.txt");
foreach (string path in paths)
using (StreamReader sr = new StreamReader(path))
{
sw.Write(sr.ReadToEnd());
sw.WriteLine("");
}
}
I think that the slow operation is in File.AppendAllText that open->write->close the merge.txt file for each txt file in directory

Restore trashed google drive files to a target folder

I need to move all files presently in my google drive's trash to a certain folder or, if that can't be done, restore them to their original location. Some mess happened and I have valuable files in the trash, some 6 gig of them, so I'd prefer to move them to a separate directory, back it up or sync locally and see those files later.
This is the script I have so far:
function moveFilesFromTrash() {
var pageSize = 200;
var files = null;
var token = null;
var cestisti = DocsList.getFolder('cestisti');
do {
var result = DocsList.getAllFilesForPaging(pageSize, token);
files = result.getFiles();
token = result.getToken();
for (var i = 0; i < files.length; i++) {
if (files[i].isTrashed == true) {
Logger.log(files[i].getName());
// files[i].setTrashed(false)
files[i].addToFolder(cestisti);
}
} while (files.length == pageSize);
}
The matter is it just does not work.
The part of the code to page through files items works, I got it from other working scripts of mine. I just don't know if it parses the trash folder or label to. I do not know if setTrashed() or addtofolder() works - I have no idea. Now the former is commented out because I would prefer to copy items instead of restoring them, but if that's not possible I can restore.
This answer has been updated due to the deprecation of DocsList, and now uses DriveApp methods throughout.
You must un-trash a file before you can move it, but doing so will accomplish what you're after. Here's the algorithm:
Get a list of files (or file ids) for all trashed files
For each file,
Undelete / un-trash the file
Move file to target folder (detail: remove file from original folders, add to target recovery folder)
This answer provides snippets of script to accomplish this; The full script is available as a Google Sheets Add-on in a gist, complete with menu and UI elements.
Recover all trashed files
This function uses DriveApp.search() to get the list of all trashed files, and then recovers them into a specified folder, rescueFldr, which will be created if not already there.
/**
* Untrash then move all trashed files to rescue folder.
* From: http://stackoverflow.com/a/14541247/1677912
*
* #returns {Number} Count of Rescued files.
*/
function rescueAllFiles() {
// Find or create target folder
var rescueFldrIterator = DriveApp.getFoldersByName(rescueFldrName);
var rescueFldr = rescueFldrIterator.hasNext() ? rescueFldrIterator.next() : DriveApp.createFolder(rescueFldrName);
// Get file iterator with all trashed files
var trashed = DriveApp.searchFiles('trashed=true');
var count = 0;
while (trashed.hasNext()) {
var file = trashed.next();
// Untrash the file
if (rescueFile( file, rescueFldr )) count++;
}
return(count);
};
Recover specific Trashed Files
If you want to have more control over the files that will be recovered, and you can't simply modify the search parameters used above, an alternative might be to list the file IDs in a spreadsheet, and have a script use that as its input.
Fill spreadsheet with trashed files
/**
* Get array of files in user's Google Drive trash, and write
* into currently active sheet.
* From: http://stackoverflow.com/a/14541247/1677912
*
* #returns {Object[]} Array of DriveApp file objects
*/
function getTrashedFiles() {
var trashedSearch = DriveApp.searchFiles('trashed=true');
var files = [];
files.push(["ID","File Name","Type","URL"]);
while (trashedSearch.hasNext()) {
var file = trashedSearch.next();
files.push([file.getId(),file.getName(),docTypeToText_(file.getMimeType()),file.getUrl()]);
}
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.getRange(1, 1, files.length, files[0].length)
.setValues(files);
return files;
}
Manual file ID collection
(Alternative to automatic retrieval, for reference only.) Since you're just doing this once, though, let's cheat. At the bottom of the Files-list page, use the API Explorer to retrieve the IDs of all your deleted files!
The output is shown just as you'd receive it if you were calling the API from a script.
Cut & paste the output into a text file, and strip it down to just the file ids. (I did that with gvim, it takes just a few commands.) Save the result as a csv file, and pull it into Drive as a spreadsheet. Here's what that will look like. (Do include a header line; the script assumes there is one.)
Process Trashed Files
Now that the list of trashed files is in a spreadsheet, scripting the recovery & move is easy.
/**
* Untrash then move files listed in spreadsheet to rescue folder.
* From: http://stackoverflow.com/a/14541247/1677912
*
* #returns {Number} Count of Rescued files.
*/
function rescueListedFiles() {
var fileList = SpreadsheetApp.getActiveSheet()
.getDataRange().getValues()
.splice(1); // Skip header line
// Find or create target folder
var rescueFldrIterator = DriveApp.getFoldersByName(rescueFldrName);
var rescueFldr = rescueFldrIterator.hasNext() ? rescueFldrIterator.next() : DriveApp.createFolder(rescueFldrName);
var count = 0;
for (var i=0; i<fileList.length; i++) {
var fileId = fileList[i][0];
var file = DriveApp.getFileById(fileId);
// Untrash the file
if (rescueFile( file, rescueFldr )) count++;
}
return( count );
};
try
if (files[i].isTrashed() == true)
I suggest that you put in some diagnostics (Logger.log) to narrow down problems
Remember, files are not in folders. Files have labels that are called folders and there can be more than one folder(label). Even 'trashed' is a label. Setting to trash just creates a trashed label that overrides the other labels(folders). The code below does not move anything. It just resets the labels(folders)
Edit: Trashed is not a label but a condition: everything else holds
for (var i = 0; i < files.length; i++) {
if (files[i].isTrashed()) { // don't forget ()
Logger.log(files[i].getName());
var folders = files[i].getParents();
// remove existing folders(labels)
for(var f = 0; f < folders.length; f++ ) {
files[i].removeFromFolder( folders[f] );
};
// remove trashed label
files[i].setTrashed(false);
// add cestisti label(add to cestisti folder)
files[i].addToFolder(cestisti);
}
}

How to Read a pre-built Text File in a Windows Phone Application

I've been trying to read a pre-built file with Car Maintenance tips, there's one in each line of my "Tips.txt" file. I've tried to follow around 4 or 5 different approaches but It's not working, it compiles but I get an exception. Here's what I've got:
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (StreamReader sr = new StreamReader(store.OpenFile("Tips.txt", FileMode.Open, FileAccess.Read)))
{
string line;
while ((line = sr.ReadLine()) != null)
{
(App.Current as App).MyTips.Insert(new DoubleNode(line));
}
}
}
I'm getting this "Operation not permitted on IsolatedStorageFileStream", from the info inside the 2nd using statement. I tried with the build action of my "Tips.txt" set to resource, and content, yet I get the same result.
Thanks in advance.
Since you've added it to your project directory, you can't read it using Isolated Storage methods. There are various ways you can load the file. One way would be to set the text file's build type to Resource, then read it in as a stream:
//Replace 'MyProject' with the name of your XAP/Project
Stream txtStream = Application.GetResourceStream(new Uri("/MyProject;component/myTextFile.txt",
UriKind.Relative)).Stream;
using(StreamReader sr = new StreamReader(txtStream))
{
//your code
}