Tanoda
Zipper.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using GILES;
6using UnityEngine;
7
8#if !UNITY_WEBGL
9using ICSharpCode.SharpZipLib.Zip;
10using NaughtyAttributes;
11#endif
12
13public class Zipper : pb_MonoBehaviourSingleton<Zipper>
14{
15
16#if !UNITY_WEBGL
17 public void Compress(string file)
18 {
19 try
20 {
21 using (ZipOutputStream OutputStream = new ZipOutputStream(File.Create(file + ".zip")))
22 {
23 // 0 - store only to 9 - means best compression
24 OutputStream.SetLevel(8);
25
26 byte[] buffer = new byte[4096];
27
28 ZipEntry entry = new ZipEntry(Path.GetFileName(file));
29
30 // Setup the entry data as required.
31
32 entry.DateTime = DateTime.Now;
33 OutputStream.PutNextEntry(entry);
34
35 using (FileStream fs = File.OpenRead(file))
36 {
37 // Using a fixed size buffer here makes no noticeable difference for output
38 // but keeps a lid on memory usage.
39 int sourceBytes;
40
41 do
42 {
43 sourceBytes = fs.Read(buffer, 0, buffer.Length);
44 OutputStream.Write(buffer, 0, sourceBytes);
45 } while (sourceBytes > 0);
46 }
47
48 // Finish is important to ensure trailing information for a Zip file is appended. Without this
49 // the created file would be invalid.
50 OutputStream.Finish();
51
52 // Close is important to wrap things up and unlock the file.
53 OutputStream.Close();
54 }
55 }
56 catch (Exception ex)
57 {
58 // No need to rethrow the exception as for our purposes its handled.
59 Debug.LogError($"Exception during processing {ex}");
60 }
61 }
62
63 public void Decompress(string file)
64 {
65 FastZip fastZip = new FastZip();
66 fastZip.ExtractZip(file, Path.GetDirectoryName(file), null);
67 }
68
69 [Button]
70 void CompressTest()
71 {
72 Compress(@"E:\Munka\vrtanoda\Assets\StreamingAssets\cube.fbx");
73 }
74 [Button]
75 void DecompressTest()
76 {
77 Decompress(@"E:\Munka\vrtanoda\Assets\StreamingAssets\cube.fbx.zip");
78 }
79#endif
80}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Definition: Zipper.cs:14
void Decompress(string file)
Definition: Zipper.cs:63
void Compress(string file)
Definition: Zipper.cs:17