Files
PepApi.Core/PepLib.Core/Utilities/ZipHelper.cs
T
ajandClaude Sonnet 4.6 85f6a50f1a feat: improve DXF export and cross-platform file handling
- PepSettings: add DrawingsDirectory property for DXF endpoint config
- DrawingDxfExporter: support subprogram calls, skip non-cut linear moves,
  remove spurious radians-to-degrees conversion on arc angles, set AC1018 DXF version
- DrawingReader: handle .loop-info/.cadsnapshot entries gracefully, derive
  DrawingInfo from loop names when .info entry is absent, use FileShare.Read
- DrawingInfoReader: use TryParse instead of Parse for material number
- Drawing: make GetLoopName public (used by DrawingDxfExporter)
- ZipHelper: use FileShare.Read to allow concurrent readers on Windows/Linux
- NestFilterData: skip status filter when value is "All"

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-27 07:23:12 -04:00

82 lines
3.0 KiB
C#

using System.IO.Compression;
using System.Text.RegularExpressions;
namespace PepLib.Utilities
{
public static class ZipHelper
{
/// <summary>
/// Returns the files that match the specified pattern.
/// </summary>
/// <param name="file">Input zip file.</param>
/// <param name="pattern">Pattern to match.</param>
/// <param name="names">Names of the files that match the pattern.</param>
/// <param name="streams">Data of the files that match the pattern.</param>
/// <returns></returns>
public static int ExtractByPattern(string file, string pattern, out string[] names, out Stream[] streams)
{
var nameList = new List<string>();
var streamList = new List<Stream>();
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Read))
{
foreach (var entry in zip.Entries)
{
if (!Regex.IsMatch(entry.FullName, pattern))
continue;
nameList.Add(entry.FullName);
var memstream = new MemoryStream();
using var entryStream = entry.Open();
entryStream.CopyTo(memstream);
memstream.Seek(0, SeekOrigin.Begin);
streamList.Add(memstream);
}
}
names = nameList.ToArray();
streams = streamList.ToArray();
return streams.Length;
}
/// <summary>
/// Returns the first file found that matches the specified file extension.
/// </summary>
/// <param name="file">Input zip file.</param>
/// <param name="extension">Extension to match.</param>
/// <param name="name">The name of the file that matches the file extension.</param>
/// <param name="stream">The data of the file that matches the file extension.</param>
/// <returns></returns>
public static bool ExtractByExtension(string file, string extension, out string name, out Stream stream)
{
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Read))
{
foreach (var entry in zip.Entries)
{
if (Path.GetExtension(entry.FullName) != extension)
continue;
var memstream = new MemoryStream();
using var entryStream = entry.Open();
entryStream.CopyTo(memstream);
memstream.Seek(0, SeekOrigin.Begin);
stream = memstream;
name = entry.FullName;
return true;
}
}
stream = null;
name = null;
return false;
}
}
}