Package 버전에 따라 무삭제 기능 추가 시작점
This commit is contained in:
@@ -1,153 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
|
||||
namespace CommonLibrary
|
||||
{
|
||||
|
||||
internal class ProgressStream : Stream
|
||||
internal class ProgressStream : Stream
|
||||
{
|
||||
private readonly Stream insideStream;
|
||||
private readonly IProgress<int> insideReadProgress;
|
||||
private readonly IProgress<int> insideWriteProgress;
|
||||
|
||||
public ProgressStream(Stream stream, IProgress<int> readProgress, IProgress<int> writeProgress)
|
||||
{
|
||||
private readonly Stream insideStream;
|
||||
private readonly IProgress<int> insideReadProgress;
|
||||
private readonly IProgress<int> insideWriteProgress;
|
||||
insideStream = stream;
|
||||
insideReadProgress = readProgress;
|
||||
insideWriteProgress = writeProgress;
|
||||
}
|
||||
|
||||
public ProgressStream(Stream stream, IProgress<int> readProgress, IProgress<int> writeProgress)
|
||||
public override bool CanRead => insideStream.CanRead;
|
||||
public override bool CanSeek => insideStream.CanSeek;
|
||||
public override bool CanWrite => insideStream.CanWrite;
|
||||
public override long Length => insideStream.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => insideStream.Position;
|
||||
set => insideStream.Position = value;
|
||||
}
|
||||
|
||||
public override void Flush() => insideStream.Flush();
|
||||
public override long Seek(long offset, SeekOrigin origin) => insideStream.Seek(offset, origin);
|
||||
public override void SetLength(long value) => insideStream.SetLength(value);
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int bytesRead = insideStream.Read(buffer, offset, count);
|
||||
insideReadProgress?.Report(bytesRead);
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
insideStream.Write(buffer, offset, count);
|
||||
insideWriteProgress?.Report(count);
|
||||
}
|
||||
}
|
||||
|
||||
public class ProgressPacker
|
||||
{
|
||||
public void Pack(string sourceDirectoryName, string packFileName, IProgress<double> progress)
|
||||
{
|
||||
sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);
|
||||
|
||||
FileInfo[] sourceFiles = new DirectoryInfo(sourceDirectoryName).GetFiles("*", SearchOption.AllDirectories);
|
||||
double totalBytes = sourceFiles.Sum(f => f.Length);
|
||||
long currentBytes = 0;
|
||||
|
||||
using (ZipArchive zipArchive = ZipFile.Open(packFileName, ZipArchiveMode.Create))
|
||||
{
|
||||
insideStream = stream;
|
||||
insideReadProgress = readProgress;
|
||||
insideWriteProgress = writeProgress;
|
||||
}
|
||||
foreach (FileInfo file in sourceFiles)
|
||||
{
|
||||
// 경로 구분자를 '/' 로 통일 (Windows 탐색기 호환)
|
||||
string entryName = file.FullName.Substring(sourceDirectoryName.Length + 1)
|
||||
.Replace(Path.DirectorySeparatorChar, '/');
|
||||
|
||||
public override bool CanRead { get { return insideStream.CanRead; } }
|
||||
public override bool CanSeek { get { return insideStream.CanSeek; } }
|
||||
public override bool CanWrite { get { return insideStream.CanWrite; } }
|
||||
public override long Length { get { return insideStream.Length; } }
|
||||
ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
|
||||
entry.LastWriteTime = file.LastWriteTime;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { return insideStream.Position; }
|
||||
set { insideStream.Position = value; }
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
insideStream.Flush();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
return insideStream.Seek(offset, origin);
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
insideStream.SetLength(value);
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int bytesRead = insideStream.Read(buffer, offset, count);
|
||||
|
||||
insideReadProgress?.Report(bytesRead);
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
insideStream.Write(buffer, offset, count);
|
||||
insideWriteProgress?.Report(count);
|
||||
using (Stream inputStream = file.OpenRead())
|
||||
using (Stream outputStream = entry.Open())
|
||||
using (Stream progressStream = new ProgressStream(inputStream,
|
||||
new BasicProgress<int>(i =>
|
||||
{
|
||||
currentBytes += i;
|
||||
progress?.Report(currentBytes / totalBytes);
|
||||
}), null))
|
||||
{
|
||||
// ✅ 방향 수정: inputStream → progressStream → outputStream
|
||||
progressStream.CopyTo(outputStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ProgressPacker
|
||||
public void UnPack(string packFileName, string destinationDirectoryName, IProgress<double> progress)
|
||||
{
|
||||
public void Pack(string sourceDirectoryName, string packFileName, IProgress<double> progress)
|
||||
using (ZipArchive zipArchive = ZipFile.OpenRead(packFileName))
|
||||
{
|
||||
sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);
|
||||
|
||||
FileInfo[] sourceFiles = new DirectoryInfo(sourceDirectoryName).GetFiles("*", SearchOption.AllDirectories);
|
||||
double totalBytes = sourceFiles.Sum(f => f.Length);
|
||||
double totalBytes = zipArchive.Entries.Sum(f => f.Length);
|
||||
long currentBytes = 0;
|
||||
|
||||
using (ZipArchive zipArchive = ZipFile.Open(packFileName, ZipArchiveMode.Create))
|
||||
foreach (ZipArchiveEntry entry in zipArchive.Entries)
|
||||
{
|
||||
// 디렉터리 엔트리 건너뜀
|
||||
if (string.IsNullOrEmpty(entry.Name))
|
||||
continue;
|
||||
|
||||
foreach (FileInfo file in sourceFiles)
|
||||
{
|
||||
string entryName = file.FullName.Substring(sourceDirectoryName.Length + 1);
|
||||
ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
|
||||
string fileName = Path.Combine(destinationDirectoryName, entry.FullName);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
|
||||
|
||||
entry.LastWriteTime = file.LastWriteTime;
|
||||
using (Stream inputStream = file.OpenRead())
|
||||
using (Stream inputStream = entry.Open())
|
||||
using (Stream outputStream = File.OpenWrite(fileName))
|
||||
using (Stream progressStream = new ProgressStream(outputStream, null,
|
||||
new BasicProgress<int>(i =>
|
||||
{
|
||||
using (Stream outputStream = entry.Open())
|
||||
{
|
||||
Stream progressStream = new ProgressStream(inputStream,
|
||||
new BasicProgress<int>(i =>
|
||||
{
|
||||
currentBytes += i;
|
||||
progress.Report(currentBytes / totalBytes);
|
||||
}), null);
|
||||
|
||||
progressStream.CopyTo(outputStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UnPack(string packFileName, string destinationDirectoryName, IProgress<double> progress)
|
||||
{
|
||||
using (ZipArchive zipArchive = ZipFile.OpenRead(packFileName))
|
||||
{
|
||||
double totalBytes = zipArchive.Entries.Sum(f => f.Length);
|
||||
long currentBytes = 0;
|
||||
foreach (ZipArchiveEntry entry in zipArchive.Entries)
|
||||
currentBytes += i;
|
||||
progress?.Report(currentBytes / totalBytes);
|
||||
})))
|
||||
{
|
||||
string fileName = Path.Combine(destinationDirectoryName, entry.FullName);
|
||||
if (entry.Name == "") continue;
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
|
||||
using (Stream inputStream = entry.Open())
|
||||
{
|
||||
using (Stream outputStream = File.OpenWrite(fileName))
|
||||
{
|
||||
|
||||
Stream progressStream = new ProgressStream(outputStream, null,
|
||||
new BasicProgress<int>(i =>
|
||||
{
|
||||
currentBytes += i;
|
||||
if (progress != null)
|
||||
{
|
||||
progress.Report(currentBytes / totalBytes);
|
||||
}
|
||||
}));
|
||||
|
||||
inputStream.CopyTo(progressStream);
|
||||
|
||||
}
|
||||
File.SetLastWriteTime(fileName, entry.LastWriteTime.LocalDateTime);
|
||||
}
|
||||
inputStream.CopyTo(progressStream);
|
||||
}
|
||||
|
||||
File.SetLastWriteTime(fileName, entry.LastWriteTime.LocalDateTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BasicProgress<T> : IProgress<T>
|
||||
{
|
||||
private readonly Action<T> actionHandler;
|
||||
|
||||
public BasicProgress(Action<T> handler)
|
||||
{
|
||||
actionHandler = handler;
|
||||
}
|
||||
|
||||
void IProgress<T>.Report(T value)
|
||||
{
|
||||
actionHandler(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BasicProgress<T> : IProgress<T>
|
||||
{
|
||||
private readonly Action<T> actionHandler;
|
||||
|
||||
public BasicProgress(Action<T> handler)
|
||||
{
|
||||
actionHandler = handler;
|
||||
}
|
||||
|
||||
public void Report(T value)
|
||||
{
|
||||
actionHandler(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user