【问题标题】:How to convert an images folder into CBZ (like comics or mangas for Ebook)如何将图像文件夹转换为 CBZ(如电子书的漫画或漫画)
【发布时间】:2021-11-20 20:37:22
【问题描述】:

我只想知道如何将图像文件夹转换为我的电子书可读的 CBZ 文件(我检查了电子书,她可以阅读这种格式)。

理想情况下,我想转换它而不必安装所有东西。只是一个脚本。

对于那些速度很快的人,我已经回答了我的问题......只是分享它。

【问题讨论】:

  • 我从未想过我会在 PowerShell 支持网站上看到漫画主题! +1 来自我(一旦你发布答案):)
  • @Daniel 这是我朋友的答案。请告诉我它是否不适合您。

标签: image powershell directory


【解决方案1】:

查看更新部分

假设您的操作系统是 Windows,我们可以使用 Batch 或 PowerShell 来完成。

对于这个,它的过程很简单,我们只需要了解 CBZ 文件一个包含图像的 ZIP 文件。所以我们将:

  1. 使用 7zip 进行压缩,因为由于某些原因,使用 WinRAR 转换的文件在我的电子书中不起作用(甚至不在图书馆中);
  2. 将扩展名从 .zip 更改为 .cbz。

我只展示 PowerShell 的,因为 .bat 脚本存在已知问题。


架构

Architecture of the directory

架构应该是:

  1. 我的第一个文件夹
    • 第一张图片
    • 第二张图片
  2. 我的第二个文件夹
    • 第一张图片
    • 第二张图片

PowerShell

这是我的“#_ImagesFolderToCBZ.ps1”中的代码

Clear-Host

# INPUT - Script folder path
$i = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# 7Zip path
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
# A list of the path of the zipped files
$listOfZippedFiles = New-Object Collections.Generic.List[String]
$listOfZippedNames = New-Object Collections.Generic.List[String]

# Ask the user if we delete the folders after their conversion
$isSdel = Read-Host -Prompt "Do you want to delete the folders after conversion [Y/N]: "

# Get the files inside the INPUT path and forEach children
Get-ChildItem "$i" | ForEach-Object {
    # Get the full path of the file
    $pathFolder = $_.FullName
    
    # If the path here is a folder
    if (Test-Path -Path "$pathFolder" -PathType Container) {
        # Set the alias
        Set-Alias 7z $7zipPath

        # If the user asked for deletion of folders
        if ("Y" -eq $isSdel.ToUpper()) {
            # Zip the content of the folder
            7z a "$pathFolder.zip" "$pathFolder\*" -sdel | FIND "ing archive"
        }
        else {
            # Zip the content of the folder
            7z a "$pathFolder.zip" "$pathFolder\*" | FIND "ing archive"
        }

        # Add the file name into the list
        $listOfZippedFiles.Add("$pathFolder.zip")
        $listOfZippedNames.Add("$_.zip")
    }

    # If the user asked for deletion of folders
    if ("Y" -eq $isSdel) {
        # Remove the now blank folders
        if( $_.psiscontainer -eq $true){
            if((gci $_.FullName) -eq $null){
                $_.FullName | Remove-Item -Force
            }
        }
    }
}

# For each zipped file
foreach ($file in $listOfZippedFiles) {
    # Change the extension to CBZ
    $dest = [System.IO.Path]::ChangeExtension("$file",".cbz")
    Move-Item -Path "$file" -Destination $dest -Force
}

# Write for the user
Write-Host "`r`n`r`nConverted:"

# Displaying the converted files by their names
foreach ($file in $listOfZippedNames) {
    $newName = [System.IO.Path]::ChangeExtension("$file",".cbz")
    Write-Host "-- $newName"
}

# Blank line
Write-Host ""
# Pause to let us see the result
Pause

输出

output

正如我们所见,文件夹已成功创建并且 没有 循环,例如:我在脚本的根文件夹中有 ZIP 文件,它们也被重命名为 CBZ 文件(我有这个循环我的批处理脚本)。

我还添加了自动删除转换后的文件夹的选项不。

显然,还有改进的余地(尤其是我们删除文件夹的方式)。我很乐意接受任何建议。


更新

我更新了我的脚本,它好多了。较少的说明,当每个文件夹真正转换时更新自身的列表(在提示中)。所以没有更多了:1)压缩所有文件夹 2)重命名它们的扩展名。

这样的代码更符合逻辑,并且对于实时显示漂亮的过程也很有用。

这是更新后的代码:

Clear-Host

# ROOT - Script folder path
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# 7Zip path
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"

# Test if 7zip is installed
if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
    throw "7 zip file '$7zipPath' not found"
}

# Ask the user if we delete the folders after their conversion
$isSdel = Read-Host -Prompt "Do you want to delete the folders after conversion [Y/N]: "

# Write for the user
Write-Host "`r`nConverted:"

# Get the files inside the INPUT path and forEach children
Get-ChildItem "$root" | ForEach-Object {
    # Get the full path of the file
    $pathFolder = $_.FullName
    
    # If the path here is a folder
    if (Test-Path -Path "$pathFolder" -PathType Container) {
        # If the user asked for deletion of folders
        if ("Y" -eq $isSdel.ToUpper()) {
            # Zip the content of the folder while deleting the files zipped
            & $7zipPath a "$pathFolder.zip" "$pathFolder\*" -sdel > $null

            # Remove the now blank folder
            if( $_.psiscontainer -eq $true){
                if((gci $_.FullName) -eq $null){
                    $_.FullName | Remove-Item -Force
                }
            }
        }
        else {
            # Zip the content of the folder
            & $7zipPath a "$pathFolder.zip" "$pathFolder\*" > $null
        }

        # Change the extension to CBZ
        $newName = [System.IO.Path]::ChangeExtension("$pathFolder.zip",".cbz")
        Move-Item -Path "$pathFolder.zip" -Destination $newName -Force

        # Tells the user this one is finished converting
        Write-Host "--" -ForegroundColor DarkGray -NoNewline
        Write-Host " $_.cbz"
    }
}

# Tells the user it's finished
Write-Host "`r`nFinished`r`n" -ForegroundColor Green

# Pause to let us see the result
Pause

更新 2

我为此创建了一个 GitHub 项目。网址在这里: https://github.com/PonyLucky/CBZ-Manga-Creator.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多