在阅读caarlos0的帖子后,我终于找到了解决方案:在Linux上使用ApacheCommonsCompression压缩文件时出现编码问题 由于某种原因,java不尊重我环境的编码,而是将其更改为cp1252。
之后,我解压缩文件,我只需要在其中输入文件夹,然后运行以下命令:
convmv --notest -f cp1252 -t utf8 * -r
并将所有内容递归转换为UTF-8。
伙计们,问题解决了。
使用apache-commons-1.8.jar库,我制作了一个可以完成此工作的工具类:
您可以在这里找到此代码:MakeTar库的GitHub存储库
import java.io.bufferedoutputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IoUtils;
/**
* The Class TarArchive.
*/
public class TarArchive {
/**
* Creates the tar of files.
*
* @param files the files
* @param tarPath the tar path
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void createTarofFiles(String[] files, String tarPath) throws IOException
{
FileOutputStream fOut = null;
bufferedoutputstream bOut = null;
TarArchiveOutputStream tOut = null;
Arrays.sort(files);
try
{
fOut = new FileOutputStream(new File(tarPath));
bOut = new bufferedoutputstream(fOut);
tOut = new TarArchiveOutputStream(bOut);
for (String file : files) {
addFileToTar(tOut, file, "");
}
}
finally
{
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}
}
/**
* Creates the tar of directory.
*
* @param directoryPath the directory path
* @param tarPath the tar path
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void createTarofDirectory(String directoryPath, String tarPath) throws IOException
{
FileOutputStream fOut = null;
bufferedoutputstream bOut = null;
TarArchiveOutputStream tOut = null;
try
{
fOut = new FileOutputStream(new File(tarPath));
bOut = new bufferedoutputstream(fOut);
tOut = new TarArchiveOutputStream(bOut);
addFileToTar(tOut, directoryPath, "");
}
finally
{
tOut.finish();
tOut.close();
bOut.close();
fOut.close();
}
}
/**
* Adds the file to tar.
*
* @param tOut the t out
* @param path the path
* @param base the base
* @throws IOException Signals that an I/O exception has occurred.
*/
private static void addFileToTar(TarArchiveOutputStream tOut, String path, String base) throws IOException
{
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
if(f.isFile())
{
tarEntry.setModTime(0);
tOut.putArchiveEntry(tarEntry);
IoUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
}
else
{
File[] children = f.listFiles();
Arrays.sort(children);
if(children != null)
{
for(File child : children)
{
addFileToTar(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
}
感谢您阅读我。
编辑:很少的校正,我已经添加了数组的种类。
编辑2:我已更正代码,以便在所有计算机上具有相同的存档。存档上计算的哈希值在任何地方都是相同的。