using Ionic.Zip;
var savedzipFile = "C:\alldocs\issues\issuesfromtoday.zip";
var selectedfolderfromDialog = "D:\MyDocs";
This is what I have. I cant use webblient because its not a url. How to I save the zip file to that selected folder location? Zip file has PDFs if that matters.
This should do the trick:
var savedzipFile = #"C:\alldocs\issues\issuesfromtoday.zip";
var selectedfolderfromDialog = #"D:\MyDocs";
using (var sourceStream = System.IO.File.Open(savedzipFile, System.IO.FileMode.Open))
{
using (var targetStream = System.IO.File.OpenWrite(selectedfolderfromDialog + #"\" + savedzipFile.Substring(savedzipFile.LastIndexOf('\\'))))
{
sourceStream.CopyTo(targetStream);
}
}
Related
I want to save multiple files in one Zip file on my disc. This Zip is created but his type is "Compressed (zipped) Folder" and is impossible to open. Error: "Windows cannot open the folder. The Compressed (zipped) Folder 'path' is invalid. Can I make it normal .Zip file somehow?
int i = 0;
await using var ms = new MemoryStream();
{
using var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
{
foreach (var s in stepwiseData)
{
//streamWriter.Write(s);
var entry = archive.CreateEntry($"Name {i.ToString()}");
i++;
using (BinaryWriter writer = new BinaryWriter(entry.Open()))
{
writer.Write(s.ToArray());
}
}
var folder = #"C:\Temp\test\";
var fileName = "TestName.zip";
var fullPath = folder + fileName;
var bytes = ms.ToArray();
File.WriteAllBytes(fullPath, bytes);
}
}
I have created hierarchical structure for managing file in bucket. Planning to create folder for each month i.e. dec-2017. There will be more than 10k pdf files in each folder.
Written C# code for getting objects from bucket. This code is working fine for accessing files that are on root of bucket. I am having issues with accessing files in folder i.e. my-bucket/dec-2017/test.pdf is not accessed using code.
Refer my bucket structure here
I am using following code, Can anyone don this before?
if (_storageService == null)
{
_storageService = CreateStorageClient();
}
ObjectsResource.GetRequest downloadRequest = null;
//string objectName = "dec-2017%2Ftest.pdf";
string objectName = "dec-2017/test.pdf";
downloadRequest = new ObjectsResource.GetRequest(_storageService, bucketName, objectName);
MemoryStream stream = new MemoryStream();
downloadRequest.Download(stream);
bytes = stream.ToArray();
Please check below sample code -
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Test.csv");
File.WriteAllText(file, "test");
GoogleCredential credential = null;
BucketConnector bucketConnector = new BucketConnector();
credential = bucketConnector.ConnectStream();
var storageClient = StorageClient.Create(credential);
string folderPath = ConfigurationParameters.FOLDER_NAME_IN_BUCKET;
using (FileStream file = File.OpenRead(localPath))
{
objectName = folderPath + Path.GetFileName(localPath);
storage.UploadObject(bucketName, objectName, null, file);
}
I have an image file in Canvas element that I get in code behind in asp.net. now I want to save it to a folder in my project but file stream always saves it to c drive. What do I do?
[WebMethod()]
public void SaveUser(string imageData)
{
//Create image to local machine.
string fileNameWitPath = path + "4200020789506" + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
// Save fileNameWitPath variable to Database.
}
Here is an example of how I save files to an Images folder in my project directory.
var fileName = "4200020789506.png";
var base64String = SOME_REALLY_LONG_STRING;
using (var s = new MemoryStream(Convert.FromBase64String(base64String)))
using (var f = new FileStream(Path.Combine(Server.MapPath("~/Images"), fileName), FileMode.Create, FileAccess.Write))
{
s.CopyTo(f);
}
Here's what I do and it works well. For you, filePath/filename = fileNameWitPath. Do this for each file you have. Hope it works for you. If you need further info, Id be glad to help.
using (var stream = File.Create(filePath + filename))
{
attachment.ContentObject.DecodeTo(stream, cancel.Token);
}
I can only imagine your path variable points to your C:\ drive.
You need to set the path variable equal to the location you want, for instance:
public void SaveUser(string imageData)
{
path = #"C:\YourCustomFolder\"; // your path needs to point to the Directory you want to save
//Create image to local machine.
string fileNameWitPath = path + "4200020789506" + ".png";
//chekc if directory exist, if not, create
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
// Save fileNameWitPath variable to Database.
}
I also included a Check to see if your directory exists, and if it does not, it will create a folder called 'YourCustomFolder' on your C drive, where it will save images.
If you would like to save your image to a folder in your project, I would recommend using Server.MapPath(~/YourFolderInApplication)
Hi this code is working for me but it is unzipping the files into the same directory. I want to unzip the files to a new directory. See code below
public void Main()
{
DirectoryInfo directorySelected = new DirectoryInfo(m_ProjectPath+m_Tool);
foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
{
Decompress(fileToDecompress);
}
}
public void Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(
currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream =
new GZipStream(originalFileStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}
}
You're copying the decompressedFileStream to newFileName which is:
string newFileName = currentFileName.Remove(
currentFileName.Length - fileToDecompress.Extension.Length);
Look at newFileName in your debugger; it's just the original file name, in the original directory, with the filename extension removed. If you want to copy it to a different directory you have to specify the directory, not copy the one from currentFileName. You want to know how to specify a directory? That will depend if you have a command-line application, a WinForm, WPF, ASP.NET, or Windows Store app.
I have a HttpPostedFile object and after the file gets uploaded locally onto the server, i want to move that temp file into a document library in sharepoint. Here is my code:
private void UploadWholeFile(HttpContext context, List<FilesStatus> statuses) {
for (int i = 0; i < context.Request.Files.Count; i++) {
HttpPostedFile file = context.Request.Files[i];
file.SaveAs(ingestPath + Path.GetFileName(file.FileName));
string fileName = Path.GetFileName(file.FileName);
}
Can anyone give me some example code for this? I have found a tutorial for Streams, but not quite sure if it would work the same in my situation
Replace the two lines starting with file.SaveAs with the following:
var myDocumentLibrary = SPContext.Current.Web.Folders["MyDocumentLibrary"];
var myFile = myDocumentLibrary.Files.Add(file.Name, file.FileContent, true);
I have a code sample for you that comes in parts:
Here is code that gets the Files content into a byte array buffer:
var file = (HttpPostedFileBase)Request.Files[0];
var buffer = new byte[file.ContentLength];
file.InputStream.Read(buffer, 0, file.ContentLength);
var root = HttpContext.Current.Server.MapPath(#"~/_temp");
var temp_file_name = "somefilename";
var path = Path.Combine(root, temp_file_name);
using (var fs = new FileStream(path, FileMode.Create))
{
using (var br = new BinaryWriter(fs))
{
br.Write(buffer);
}
}