Пример #1
0
    public override IEnumerator Execute(UTContext context)
    {
        var theBaseDirectory = baseDirectory.EvaluateIn(context);

        if (string.IsNullOrEmpty(theBaseDirectory))
        {
            theBaseDirectory = UTFileUtils.ProjectAssets;
        }

        var theOutputFileName = outputFileName.EvaluateIn(context);

        if (string.IsNullOrEmpty(theOutputFileName))
        {
            throw new UTFailBuildException("Output file name must be set", this);
        }


        if (!theOutputFileName.EndsWith(".zip"))
        {
            Debug.LogWarning("Output filename should end with .zip.", this);
        }

        if (!appendToExistingFile.EvaluateIn(context))
        {
            if (File.Exists(theOutputFileName))
            {
                if (UTPreferences.DebugMode)
                {
                    Debug.Log("Deleting existing ZIP file: " + theOutputFileName);
                }
                File.Delete(theOutputFileName);
            }
        }

        var theIncludes = EvaluateAll(includes, context);
        var theExcludes = EvaluateAll(excludes, context);

        var fileList = UTFileUtils.CalculateFileset(theBaseDirectory, theIncludes, theExcludes, UTFileUtils.FileSelectionMode.Files);

        if (fileList.Length == 0)
        {
            throw new UTFailBuildException("There is nothing to ZIP.", this);
        }

        Debug.Log("Zipping " + fileList.Length + " files.", this);

        UTFileUtils.EnsureParentFolderExists(theOutputFileName);

        var doFlatten = flattenStructure.EvaluateIn(context);
        var theBaseFolderInZipFile = baseFolderInZIPFile.EvaluateIn(context);

        using (ZipFile zf = new ZipFile(theOutputFileName)) {
            foreach (var file in fileList)
            {
                if (UTPreferences.DebugMode)
                {
                    Debug.Log("Zipping: " + file, this);
                }
                if (doFlatten)
                {
                    zf.AddFile(file, theBaseFolderInZipFile);
                }
                else
                {
                    var relativePath = UTFileUtils.StripBasePath(UTFileUtils.GetParentPath(file), theBaseDirectory);
                    zf.AddFile(file, UTFileUtils.CombineToPath(theBaseFolderInZipFile, relativePath));
                }
                yield return("");
            }
            zf.Save();
        }

        Debug.Log("ZIP file created at " + theOutputFileName, this);
        yield return("");
    }