1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| 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)
{
// Demande à l'utilisateur le chemin absolu d'un fichier ODT
Console.WriteLine("Merci de renseigner le chemin absolu d'un fichier ODT:");
string odtFilePath = Console.ReadLine();
// Lis le contenu du fichier ODT
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);
// Convertit le stream en string
StreamReader reader = new StreamReader(s);
string contentXml = reader.ReadToEnd();
// Recherche tous les liens qui commencent par "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);
// Traite chaque lien trouvé
foreach (Match match in matches)
{
string link = match.Value;
string[] parts = link.Split(new string[] { "aspx?" }, StringSplitOptions.None);
string queryString = parts[parts.Length - 1];
string[] parameterParts = Regex.Split(link, "(?<=aspx\\?)");
// Télécharge le document intranet correspondant
string newFileName = Path.GetFileName(new Uri(link).LocalPath);
string folderName = Path.GetFileNameWithoutExtension(odtFilePath);
string subFolderName = "PJ - " + folderName;
string localFilePath = "C:/PiecesJointes/";
string onlineFilePath = "https://com.test.fr/files/test/test/" + queryString;
Directory.CreateDirectory(Path.GetDirectoryName(localFilePath));
using (WebClient client = new WebClient())
{
client.DownloadFile(link, localFilePath);
}
// Remplace le lien par le chemin du document téléchargé
string newLink = localFilePath.Replace("\\", "/");
contentXml = contentXml.Replace(link, newLink);
}
// Met à jour le content.xml dans le fichier ZIP initial
byte[] contentXmlBytes = System.Text.Encoding.UTF8.GetBytes(contentXml);
ms = new MemoryStream();
zf.BeginUpdate();
// Ajoute le contenu mis à jour au fichier ZIP
ZipOutputStream zos = new ZipOutputStream(ms);
zos.UseZip64 = UseZip64.Off;
zos.IsStreamOwner = false;
// Ajoute l'entrée pour le fichier content.xml
zos.PutNextEntry(new ZipEntry(entry.Name));
StreamUtils.Copy(new MemoryStream(contentXmlBytes), zos, new byte[4096]);
// Traite chaque entrée du fichier ODT original
foreach (ZipEntry origEntry in zf)
{
// Ignore l'entrée pour le fichier content.xml car il a déjà été ajouté
if (origEntry.Name == entry.Name) continue;
// Ajoute l'entrée au nouveau fichier ZIP
zos.PutNextEntry(new ZipEntry(origEntry.Name));
StreamUtils.Copy(zf.GetInputStream(origEntry), zos, new byte[4096]);
}
zos.Close();
// Termine la mise à jour du fichier ZIP
zf.CommitUpdate();
zf.Close();
// Renomme et enregistre le fichier ODT mis à jour
string updatedFilePath = Path.Combine(Path.GetDirectoryName(odtFilePath), "updated_" + Path.GetFileName(odtFilePath));
using (FileStream stream = new FileStream(updatedFilePath, FileMode.Create))
{
ms.Position = 0;
ms.WriteTo(stream);
}
Console.WriteLine("Le fichier ODT a été mis à jour avec succès et enregistré sous le nom : " + updatedFilePath);
Console.ReadLine();
}
}
} |
Partager