Gekidow
Member
- Joined
- Apr 20, 2023
- Messages
- 12
- Programming Experience
- Beginner
Hello, I have a problem, my program reads an ODT file and downloads the links inside it, these links correspond to PDF files available on an intranet. The problem is that as a result of the program, I don't have 128 pdf that are downloaded but I have 128 files (which correspond well in terms of name to what I am supposed to have) without extension, and which are all 18 kb of size. My question is then the following: Why do I not have PDF files in output but files without extension as on the screenshot? Is it a redirect problem ? I also tried with DownloadFile method and i have the same result The System.Diagnostics.Process.Start(link); method works but I can't rename the files because the program only execute them and doesn't downloads them(the browser downloads them). PS : i'm on .NET 3.5 and Visual Studio 2010
Here is my code :
Here is my code :
C#:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace PrepareDocForExternalUse
{
class Program
{
static void Main(string[] args)
{
// Prompts the user for the absolute path to an ODT file
Console.WriteLine("Please enter the absolute path of an ODT file:");
string odtFilePath = Console.ReadLine();
// Read the contents of the ODT file
byte[] content = File.ReadAllBytes(odtFilePath);
MemoryStream ms = new MemoryStream();
ms.Write(content, 0, content.Length);
ZipFile zf = new ZipFile(ms);
zf.UseZip64 = UseZip64.Off;
zf.IsStreamOwner = false;
ZipEntry entry = zf.GetEntry("content.xml");
Stream s = zf.GetInputStream(entry);
// Convert stream to string
StreamReader reader = new StreamReader(s);
string contentXml = reader.ReadToEnd();
// Search for all links that start with "applnet.test.fr"
string pattern = @"http://applnet\.test\.fr/GetContenu/Download\.aspx\?p1=.*?;p2=.*?;p5=.*?;p6=NOPUB";
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(contentXml);
Directory.CreateDirectory(Path.GetDirectoryName(odtFilePath));
// Process each link found
foreach (Match match in matches)
{
string link = match.Value;
string[] parts = link.Split(new string[] { "aspx?" }, StringSplitOptions.None);
string queryString = parts[parts.Length - 1];
// Download the corresponding intranet document
string folderName = Path.GetFileNameWithoutExtension(odtFilePath);
string subFolderName = "PJ - " + folderName;
string fileName = queryString;
string localFilePath = "C:/PiecesJointes/" + fileName;
string onlineFilePath = "https://com.test.fr/files/test/test/" + queryString;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.AllowAutoRedirect = false;
request.Method = "GET";
request.ContentType = "application/pdf";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
byte[] buffer = new byte[4096];
int bytesRead = 0;
FileStream fileStream = new FileStream(localFilePath, FileMode.Create);
do
{
bytesRead = stream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
fileStream.Close();
response.Close();
// Replace the link with the path of the downloaded document
string newLink = localFilePath.Replace("\\", "/");
contentXml = contentXml.Replace(link, onlineFilePath);
}
// Updates the content.xml in the initial ZIP file
byte[] contentXmlBytes = System.Text.Encoding.UTF8.GetBytes(contentXml);
ms = new MemoryStream();
zf.BeginUpdate();
// Add updated content to ZIP file
ZipOutputStream zos = new ZipOutputStream(ms);
zos.UseZip64 = UseZip64.Off;
zos.IsStreamOwner = false;
// Add entry for content.xml file
zos.PutNextEntry(new ZipEntry(entry.Name));
StreamUtils.Copy(new MemoryStream(contentXmlBytes), zos, new byte[4096]);
// Processes each entry from the original ODT file
foreach (ZipEntry origEntry in zf)
{
// Ignore the entry for the content.xml file because it has already been added
if (origEntry.Name == entry.Name) continue;
// Add entry to new ZIP file
zos.PutNextEntry(new ZipEntry(origEntry.Name));
StreamUtils.Copy(zf.GetInputStream(origEntry), zos, new byte[4096]);
}
zos.Close();
// Finish updating the ZIP file
zf.CommitUpdate();
zf.Close();
// Renames and saves the updated ODT file
Guid g = Guid.NewGuid();
string updatedFilePath = Path.Combine(Path.GetDirectoryName(odtFilePath), g + "_" + Path.GetFileName(odtFilePath));
using (FileStream stream = new FileStream(updatedFilePath, FileMode.Create))
{
ms.Position = 0;
ms.WriteTo(stream);
}
Console.WriteLine("The ODT file has been successfully updated and saved as: " + updatedFilePath);
Console.ReadLine();
}
}
}